aspose file tools*
The moose likes Beginning Java and the fly likes Bytes retrieval Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bytes retrieval" Watch "Bytes retrieval" New topic
Author

Bytes retrieval

Anjali Malar
Greenhorn

Joined: Apr 09, 2011
Posts: 6

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

Joined: Oct 13, 2005
Posts: 36478
    
  16
. . . and what do you get when you print out the String c?
Anjali Malar
Greenhorn

Joined: Apr 09, 2011
Posts: 6

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
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

Yes, that's right. So is your question resolved, then?
Anjali Malar
Greenhorn

Joined: Apr 09, 2011
Posts: 6

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Is it really two characters at a time, Rob?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Oct 13, 2005
Posts: 36478
    
  16
Yes, I see what you mean, Rob.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bytes retrieval
 
Similar Threads
converting a string to bytes
New challenge equals == hashcode(m confused)
No. of String Objects
Addition of float and String
operators