Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bytes retrieval

 
Anjali Malar
Greenhorn
Posts: 6
Java MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have concatenated bytes with String. If I want to retrieve the concatenated bytes is it possible?
Below is a sample code. I just got this doubt.



I would like to get back b from c . Is that possible ?

Thanks !
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and what do you get when you print out the String c?
 
Anjali Malar
Greenhorn
Posts: 6
Java MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the Welcome and the byte representation in String format.


But I want to use B@19821f as bytes to get back my "hello world" . Like if I put the retrieved byte from String c as below , then I should get back hello world.



 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. So is your question resolved, then?
 
Anjali Malar
Greenhorn
Posts: 6
Java MyEclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. When I get the [B@19821f from String c and form a new String "hello world" is not retrieved.

Below is the complete problem ....


In line 7, I should get "hello world" back. But its not the case here. It still printing [B@19821f. But I want the original string.

 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not going to happen if "c" remains formatted like that. The "[B@19821f" means an array ("[") of bytes ("B") with hexidecimal hash code 19821f. For arrays it's 100% impossible to get the contents when all you have is a hash code. The hash code doesn't represent the array elements, just the array itself. And that array may no longer exist.

You need to turn the byte[] into a String in a better way. The easy way is to use "new String(b)" but that's too easy. You probably want something like transforming the byte[] into a hexadecimal String based on its elements. If you use our search you should be able to find some threads about that. I can clearly remember a small discussion we had in this thread.

That's converting from byte[] to String. You then need to be able to convert the String back into a byte[]. That's actually very easy. Split the String into pieces of 2 characters each. Then use Short.parseShort(x, 16) to turn these into shorts. Cast these into bytes. You will need the shorts because the hexadecimal representation of -1 is FF which would result in 255, and that's not within the range of byte. By casting the short 255 into a byte you get a byte with value -1 again.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it really two characters at a time, Rob?
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A byte? Definitely. The value ranges from -128 to 127. In unsigned form (which does not exist in Java, granted) that's 0 (00) to 255 (FF).

With hexadecimal representation you can't go using 1 character for bytes smaller than 16; after all, would FF be one value of 255 or two values of 15 each?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see what you mean, Rob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic