> Note that 3F is the character "?", which is usually the result of converting
> bytes that aren't in your target encoding.
Good one. I was going to say that it's a wonder -1 is converted at all.
I would have expected an Exception.
I dunno what to do about the original poster's issue. Java has no
unsigned numbers, period. I assume he wants -1 to be converted to 255,
but a clean way to do this escapes me. I don't convert text all that often.
if ( b < 0 )
b += 256;
... then convert b, might work...
Patricia Shanahan - 01 Mar 2007 23:42 GMT
>> Note that 3F is the character "?", which is usually the result of
>> converting bytes that aren't in your target encoding.
[quoted text clipped - 11 lines]
>
> ... then convert b, might work...
If "correct" means "value of char is value of byte bit pattern treated
as unsigned binary" then the following works:
static char byteToChar(byte b){
return (char)(b & 0xff);
}
However, the mapping to 3f suggests that the bytes are being treated as
8 bit characters, rather than numbers, so the OP may mean something
entirely different by "correct".
Patricia
Mike Schilling - 02 Mar 2007 00:16 GMT
>> Note that 3F is the character "?", which is usually the result of
>> converting bytes that aren't in your target encoding.
>
> Good one. I was going to say that it's a wonder -1 is converted at
> all. I would have expected an Exception.
It depends on the encoding used. I think US_ASCII will convert 0xFF to '?'.
ISO-8859-1 should convert it to 0xFF, because (I think) all 8-bit pattenrs
are valid and map to the first 256 Unicode characters. UTF-8 probably maps
to '?' also, because 0xff isn't a valid start character.