You are not logged in.

#1 2007-04-29 18:34:12

major
Member
From: Ockelbo, Sweden
Registered: 2005-12-30
Posts: 25

java byte[] to float

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

#2 2007-04-29 20:25:43

[vEX]
Member
From: Sweden
Registered: 2006-11-23
Posts: 450

Re: java byte[] to float

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

#3 2007-04-29 21:29:48

major
Member
From: Ockelbo, Sweden
Registered: 2005-12-30
Posts: 25

Re: java byte[] to float

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 smile

And... yes, read that thread before i posted here, couldnt get it to work sad

Offline

#4 2007-04-29 23:16:08

Ruckus
Member
Registered: 2007-02-17
Posts: 204

Re: java byte[] to float

major wrote:

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 smile

And... yes, read that thread before i posted here, couldnt get it to work sad

What problems where you having with it? (ByteBuffer)

Offline

#5 2007-04-30 00:17:29

major
Member
From: Ockelbo, Sweden
Registered: 2005-12-30
Posts: 25

Re: java byte[] to float

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 smile

Offline

#6 2007-04-30 03:04:51

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: java byte[] to float

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

#7 2007-04-30 04:29:38

neotuli
Lazy Developer
From: London, UK
Registered: 2004-07-06
Posts: 1,204
Website

Re: java byte[] to float

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 smile


The suggestion box only accepts patches.

Offline

#8 2007-04-30 09:11:45

major
Member
From: Ockelbo, Sweden
Registered: 2005-12-30
Posts: 25

Re: java byte[] to float

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 smile

Offline

#9 2007-05-01 14:07:12

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: java byte[] to float

major wrote:

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

Board footer

Powered by FluxBB