Convert char to byte, without loss of information
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Edward_Hopper
Posted On:   Sunday, June 18, 2006 07:57 AM

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 Byte long and byte in 1 Byte 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.    More>>

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 Byte long and byte in 1 Byte 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 such a "daily" problem.



I am grateful to get a lot of hints and examples. Please!



Thank you and


kind regards


edho

   <<Less

Re: Convert char to byte, without loss of information

Posted By:   Anonymous  
Posted On:   Thursday, August 3, 2006 11:20 AM

I think you should read the JavaDoc of String.getBytes(«charSet»).

That does the work for you: convert a Unicode String to a byte array. How it processes 'high' characters is determined by the character set specified.

I usually use "UTF-8", but any supported character set should be fine as long as you use the same one in the String constructor on the 'reconstructing' end.

Re: Convert char to byte, without loss of information

Posted By:   Nicol_Saraova  
Posted On:   Wednesday, August 2, 2006 03:44 AM

char c = 200;
byte b = c & 0xff;


where b will be interpreted as signed value

Re: Convert char to byte, without loss of information

Posted By:   Robert_Lybarger  
Posted On:   Sunday, June 18, 2006 09:29 AM

Welcome to the pain of architecture- and system-dependent primitives. Java (well, Sun) decided all numeric primitives are just always signed so one need not worry what value a given string of bits represents. You may need to use a 'short' instead of a 'byte' if you don't want to worry about the signed/unsigned problem between your legacy system and your Java code. That isn't without its own set of problems later, of course. (If you're putting data back into the legacy system, you must be sure you haven't used too much of the short's available numeric range.) You might need to get familiar with Java's bit manipulation operators so you can directly from a "char" to a "short" (where the short is your pseudo-unsigned-byte). See the bit logic operators &, |, and the bit shift operators >>, >>>, and <<.
About | Sitemap | Contact