Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

convert byte to char

Thread view: 
matafbtt - 01 Mar 2007 11:37 GMT
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 ?
Chris Dollin - 01 Mar 2007 11:37 GMT
> 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.

Signature

Chris "electric hedgehog" Dollin
"It was the first really clever thing the King had said that day."
/Alice in Wonderland/

Patricia Shanahan - 01 Mar 2007 12:22 GMT
> Hi All,
>
[quoted text clipped - 3 lines]
>
> any help ?

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

Patricia
Mike Schilling - 01 Mar 2007 17:55 GMT
> 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.
Mark Space - 01 Mar 2007 22:59 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.

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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.