You are not logged in.
Pages: 1
Hi, i've started to learn java, and as a "fun project" im doing a "hlds-server-remote-rcon-tool-thingine"
Protocol spec: http://www.valve-erc.com/srcsdk/Code/Ne … ueries.htm
Uner the A2S_PLAYER it says i should get a float as connectiontime in seconds. (I get username, kills just fine).
But i cant get the seconds connected?
So, the question, how to convert a byte[] to a float in java?
Offline
Well, there probably is a better way, but you could make it into a string and then convert that to a float; String s = new String(theBytes); float f = Float.valueOf(s);
Had it been just one byte you could have used Byte.floatValue(); Byte b = new Byte(theByte); float f = b.floatValue();
EDIT: Last post in this thread http://forum.java.sun.com/thread.jspa?t … ID=2789673 seems to have a nicer solution.
Last edited by [vEX] (2007-04-29 20:28:56)
PC: Antec P182B | Asus P8Z77-V PRO | Intel i5 3570k | 16GB DDR3 | GeForce 450GTS | 4TB HDD | Pioneer BDR-207D | Asus Xonar DX | Altec Lansing CS21 | Eizo EV2736W-BK | Arch Linux x86_64
HTPC: Antec NSK2480 | ASUS M3A78-EM (AMD 780G) | AMD Athlon X3 425 | 8GB DDR2 | GeForce G210 | 2TB HDD | Arch Linux x86_64
Server: Raspberry Pi (model B) | 512MB RAM | 750GB HDD | Arch Linux ARM
Offline
Thanx for replying.
Well. i dont want to do unnecisary convertions between datatypes, must be a better way then to convert to string before you convert to float
And... yes, read that thread before i posted here, couldnt get it to work
Offline
Thanx for replying.
Well. i dont want to do unnecisary convertions between datatypes, must be a better way then to convert to string before you convert to float
And... yes, read that thread before i posted here, couldnt get it to work
What problems where you having with it? (ByteBuffer)
Offline
well ive soled it now.
The solution:
public static float arr2float (byte[] buf, int pos){
byte[] asdf=new byte[4];
for(int i = 0 ; i < 4; i++)
asdf[i] = buf[pos+4-i];
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(asdf);
float value = bb.getFloat(0);
return value;
}
So, the last example on that page was right, it was the order of the byte[] that was wrong
Offline
public static float arr2float (byte[] buf, int pos){
byte[] asdf=new byte[4];
for(int i = 0 ; i < 4; i++)
asdf[i] = buf[pos+4-i];
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(asdf);
float value = bb.getFloat(0);
return value;
}
Cleaner:
public static float arr2float (byte[] buf, int pos){
byte[] asdf=new byte[4];
System.arraycopy(buf, pos, asdf, 0, 4);
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(asdf);
return bb.getFloat(0);
}
May also be able to just use:
public static float arr2float (byte[] buf, int pos){
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(buf, pos, 4);
return bb.getFloat(0);
}
Last edited by Cerebral (2007-04-30 03:08:24)
Offline
I wrote a python script to do these queries on counter strike source servers. I can post it if you want, though I know you probably want to dabble in java
The suggestion box only accepts patches.
Offline
Cerebral:
Yes, i had arraycopy first to, but the order of the byutes was wrong.
neotuli:
Well. ivr already done the python one
Did a simple GUI in qt for it to
Offline
Cerebral:
Yes, i had arraycopy first to, but the order of the byutes was wrong.
DOH - totally missed that... yeah, if you want to reverse the input byte-order I guess your way is the best.
Offline
Pages: 1