Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java char contains value > 255?

Tags:

java

char

I get a char array from a socket :

char[] cbuf = new char[3];
inputStream.read(cbuf, 0, 3); // read 3 chars in buffer "cbuf", offset = 0

Then when I print that :

System.out.println("r:"+(int)cbuf[0]+" g:"+(int)cbuf[1]+" b:"+(int)cbuf[2]);

I get at some point :

...
r:82 g:232 b:250
r:82 g:232 b:250
r:66 g:233 b:8224

The 8224 value is way more than 255, how can a char contain this value ???

Thank you

like image 405
Matthieu Napoli Avatar asked May 18 '26 23:05

Matthieu Napoli


1 Answers

The char primitive in Java in 16 bits wide, to accommodate characters outside the standard ASCII range, using Unicode.

It looks like you're trying to store RGB values in a char[3]. May I suggest a byte[3], or java.awt.Color?

Color c = new Color(255, 255, 240);
like image 190
Michael Petrotta Avatar answered May 21 '26 14:05

Michael Petrotta



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!