convert byte to char

C

Chris Dollin

matafbtt said:
I have a byte with value -1. when converting to char I expect to get
the hex value of FF. but after converting I get 3F. All the other
characters are converted correctly.

After intensive scrutiny of your post, I can't find any broken code.
Or any code at all. Hint.

Test cases would be good.
 
M

matafbtt

Hi All,

I have a byte with value -1. when converting to char I expect to get
the hex value of FF. but after converting I get 3F. All the other
characters are converted correctly.

any help ?
 
P

Patricia Shanahan

matafbtt said:
Hi All,

I have a byte with value -1. when converting to char I expect to get
the hex value of FF. but after converting I get 3F. All the other
characters are converted correctly.

any help ?

Any data? Specifically, how did you do the conversion, and how is
"correctly" defined in your context?

Patricia
 
M

Mike Schilling

matafbtt said:
Hi All,

I have a byte with value -1. when converting to char I expect to get
the hex value of FF. but after converting I get 3F. All the other
characters are converted correctly.

How are you doing the conversion?

Note that 3F is the character "?", which is usually the result of converting
bytes that aren't in your target encoding.
 
M

Mark Space

Mike said:
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...
 
P

Patricia Shanahan

Mark said:
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...

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
 
M

Mike Schilling

Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top