Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get two raw bytes from a single char

Size of char in Java is two bytes. How can I get raw bytes from a char variable?

var ch = '文';
var b1 = // ?
var b2 = // ?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can perform bitwise arithmetic on char to access the individual bytes:

var b1 = (byte) ch;
var b2 = (byte) (ch >> 8);
System.out.printf("%02x %02x\n", b1, b2);
// 87 65
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading