Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert INT(0-255) to UTF8 char in Java

Tags:

java

integer

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 !
like image 266
Nils Avatar asked Mar 10 '26 06:03

Nils


1 Answers

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.

like image 117
kennytm Avatar answered Mar 11 '26 22:03

kennytm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!