• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert char to byte, without loss of information

 
Andrew Paul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I get from an archive-server via http a string with archive-data(legacy-system).

The smallest part of a string is char (charAt(y)). char is 2 bit long and byte in 2 bit long. In java a byte has the range from -128 to 127.

In the return-String from the server are values greater than 127. For example 192.

So if I cast the char "192" to byte information get lossed? I emphasize that I do not have a problem to cast. The program works but the output is "wrong - in the sense - that a byte can't hold a value greater than 127.

In other languages like C you have an unsigned byte ( 0 to 255 ). But not in Java.

Is that problem soluble in Java?

How can I convert the string to an byte-array or the single char to a byte if a java-byte is always signed.

I assume that my java-thinking is wrong!!!???

For me it is inconceivable that java is not able to solve the problem.

I am grateful to get a lot of hints and tipps from you - the most experienced java-user in the world!

Thank you and
kind regards
Andrew Paul
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"keepitsimple",

We're pleased to have you here with us at JavaRanch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Changing the display name from "keepitsimple" to "Andrew Paul" would be perfect.

Thanks,
Jeanne
Forum Bartender
 
Andrew Paul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jeanne Boyarsky!

Thanks for the hint!

I've done it (change the displayed name).

So I'm looking forward for an answer.

Bye
Andrew
 
Henry Wong
author
Marshal
Pie
Posts: 21431
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cast a char to a byte, you will definitely lose the high byte -- nothing you can do about it, unless you want to cast it to a short instead.

As for the low byte, it should cast correctly, but not as a byte value of 192. If you try to print it, it will show up as a negative number. However, it should be the same bit pattern as the low byte of the char, so you be able to parse it and even write it out, in binary.

Henry
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The premise of the question is incorrect. You do not receive a String from a HTTP server - you receive a sequence of bytes - order guaranteed by TCP (etc. etc.). Once you have these bytes, treat them as bytes in their rawest form or as characters after applying a character encoding. You might be inadvertantly calling an API (or language feature) that converts the bytes to characters before the point that you wish to convert the characters back to bytes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic