• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bytes retrieval

 
Greenhorn
Posts: 6
MyEclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and what do you get when you print out the String c?
 
Anjali Malar
Greenhorn
Posts: 6
MyEclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's right. So is your question resolved, then?
 
Anjali Malar
Greenhorn
Posts: 6
MyEclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it really two characters at a time, Rob?
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I see what you mean, Rob.
reply
    Bookmark Topic Watch Topic
  • New Topic