How to read the Bytes from a ByteBuffer ?
Hi
I stored a float into a ByteBuffer and I would like to read the Bytes (not float value) from the ByteBuffer.
I tried to do this
float Numb = 20;
buffer.putFloat(Numb);
byte a[] = new byte[4];
buffer.get(a, 0, a.length);
System.out.println(a.toString());
But I get in the console : [B@3e25a5
Thanks
Stan
[374 byte] By [
Stan84a] at [2007-11-26 13:41:26]

for(int i = 0; i < a.length; i++)
{
System.out.print(a[i]);
}
System.out.println();
or
for(byte b : a)
{
System.out.print(b);
}
System.out.println();
you are calling toString on an object (an array of type byte), thus getting the string representation of the array object itself, instead of the values of the elements of the array.
~Tim
Java arrays do not provide a toString implementation.Decide how you want to see your bytes, and print them out.A nice easy solution is usingjava.util.Arrays.toString(bytes)
Hi Tim !thx for your answer. It seems to work but I get now in the console this: 65-9600Is it a format problem ?regardsStan
> It seems to work but I get now in the console this: 65-9600What value do you expect?Try putting a space between each value:System.out.print(a[i]);System.out.print(" ");so you at least know where the byte values separate.
> Hi Tim !
>
> thx for your answer. It seems to work but I get now
> in the console this: 65-9600
>
> Is it a format problem ?
>
> regards
>
> Stan
Sort of. The values are 65,-96,0,0 and when you print them out, they are all squished together, since the print doesn't contain any formatting.
~Tim
Hi IanSchneider !thanks for you answer !I tried your solution too but I get this in the console too: B@3e25a5Do you have some idea ?ThanksStan
Yeah I have some idea:byte[] b = new byte[] {1,2,3};System.out.println(Arrays.toString(b));
Hi,
you are right, the numbers are all squished.
But they aren't the expected numbers... I expect to print the binary representation from my Bytebuffer.
I stored a float into a ByteBuffer, so that the float would be convert in Byte. And I try to show the Bytes which are into the ByteBuffer.
Thx
Stan
Hi IanSchneider,thx for your answer ! it works ! but I don't expect these numbers....regardsStan
Then most likely your expectation is wrong. This could be because you didn't realize that bytes are signed in Java, or because you got your endian-ness wrong, or because you don't understand what the bytes are in a float, or for some other reason. But it's impossible to tell without knowing what you did, what you expected to see, and what you actually saw.
Hi DrClap,Thx for your answer ! Perhaps I did not clearly formulate my problem: a float is composed of 32 bits, so my objective is to print the 32bits,which are in my bytebuffer, in my console.Do you see what I mean ?ThxBest regards Stan
I already knew that. Obviously you don't understand what I mean. You said"but I don't expect these numbers...."Okay. So what numbers do you expect? What makes you think they aren't the right numbers? We aren't mind-readers here, you know.
It seems to be curious, but the numbers that I expected were more a binary sequence....
> It seems to be curious, but the numbers that I
> expected were more a binary sequence....
I have asked you twice what you expect. So that answer tells me that you haven't a clue what to expect. Am I right? Or does that "binary sequence" business mean that you just want the bytes represented in hexadecimal or binary or something?
yes you are right, I just want the bytes represented in binary.
Okay. Well, Integer.toBinaryString(int) will convert a number to its binary equivalent (as a string of course). Combine that with the code given to you in Reply 1, and insert some spaces if you want to keep the bytes separate in the output.