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 ;)
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.
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.
Joined: Feb 08, 2005
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.
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.)
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':
Joined: Feb 08, 2005
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........
subject: Bytearray to ascii string? Remove padding?