views:

66

answers:

2

Hi, since I need to control some devices, I need to send some bytes to them. I'm creating those bytes by putting some int values together (and operator), creating a byte and finally attaching it to a String to send it over the radio function to the robot.

Unfortuantely Java has some major issues doing that (unsigned int problem)

Does anybody know, how I can convert an integer e.g.

x = 223;

to an 8-bit character in Java to attach it to a String ?

char = (char)x; // does not work !! 16 bit !! I need 8 bit !

A: 

A char is 16-bit in Java. Use a byte if you need an 8-bit datatype.

See How to convert Strings to and from UTF8 byte arrays in Java on how to convert a byte[] to String with UTF-8 encoding.

KennyTM
A: 

Sending a java.lang.String over the wire is probably the wrong approach here, since Strings are always 16-bit (since Java was designed for globalization and stuff). If your radio library allows you to pass a byte[] instead of a String, that will allow you to send 8-bit values without needing to worry about converting to UTF8. As far as converting from an int to an unsigned byte, you'll probably want to look at this article.

btown