File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Convert char to byte, without loss of information Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert char to byte, without loss of information" Watch "Convert char to byte, without loss of information" New topic
Author

Convert char to byte, without loss of information

Andrew Paul
Greenhorn

Joined: Jun 17, 2006
Posts: 5
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


Diplomacy is to say "Goood doggie" - until you find a stone.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29245
    
139

"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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Andrew Paul
Greenhorn

Joined: Jun 17, 2006
Posts: 5
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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert char to byte, without loss of information
 
Similar Threads
help clarify signed, long, int
How to Return Extended ASCII Decimal Code
doubt on char assignment
Byte to string Conversion
Trouble with system.in manipulation