aspose file tools*
The moose likes I/O and Streams and the fly likes Bytearray to ascii string? Remove padding? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Bytearray to ascii string? Remove padding?" Watch "Bytearray to ascii string? Remove padding?" New topic
Author

Bytearray to ascii string? Remove padding?

Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
I'm sure this is simple but after a Google I can't find the answer. I'm also sure I'm being quite thick.




This produces the desired output except there are strange characters at the end of the valid string.
In the spec I'm working from it states "null characters are used as padding". I'm assuming this is what the strange characters after the serial number are.
How do I remove them?

Like I say I'm sure this is simple and I'm being a bit thick so please take pity on me ;)

Many thanks in advance

Alan
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Have you tried String.trim() already? Or is it allowed to have the String end in whitespace?

If so, loop from the end of the String until the first non-'\0' character, then chop off those using String.substring.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
I've tried trim but doesn't remove the strange characters at the end. I'm assuming because trim looks for white space which isn't a null character?
I'll have a try with your other suggestion a bit later.

Thanks for the reply!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

String.trim removes leading and trailing characters that are, in ASCII, smaller than or equal to a space. In other words, if the int representation is 32 or less. '\0' is the same as 0, so that's apparently not the strange character you receive. Or there are other non-"whitespace" characters after it.
Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
I really appreciate your response to this, and thanks for clarifying the trim function.

Just to elaborate a bit on what's happening, if I copy and paste the output in the console window (I'm using the eclipse ID)
I don't see the funny characters? For example I've copied and pasted this directly from the console.

Unit SerialNumber=1756067


However the console window displays the attached.

This might be nothing to worry about as all I'm going to do is write that value to a file, where I'm assuming the 'funny' characters will vanish. It's just sparked my curiosity now, possibly as this is my first time playing with Java and Eclipse for many years. (See dates of embarrassing previous forum posts.)




[Thumbnail for console.png]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

They won't vanish. The entire String will be written, and that includes the weird characters at the end.

Can you execute the following code for the String and print the results:
It will start with 49, 55, 53, 54, 48, 54 and 55 - the ASCII values for 1, 7, 5, 6, 0, 6 and 7. After that I think will be a 0, which means '\0' - the NULL character.

If that is the case you should go through the String as I did, keep the current index and break when you find '\0':
Alan Hollis
Greenhorn

Joined: Feb 08, 2005
Posts: 19
That is spot on thank you very very much for your help. I owe you a beer.
However I'm surprised that if the data I'm extracting is actually a null padded string why using the trim function didn't work??


is actually a null padded string why using the trim function didn't work??


Guess I answered my own question here didn't I........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bytearray to ascii string? Remove padding?