java - Int to byte array - byte shifting -


i'm trying convert int (max. 65535) 2 bytes array. in c used uint16, java doesn't know unsigned values.

to convert bytes array tried use this:

byte[] data = { (byte) ((num >> 8) & 0xff), (byte) (num & 0xff) }; 

but get: [63, -49] instead of: [63, 207], if use 16335 value. there way in java?

i need unsigned byte in byte array send using outputstream

you can use java's nio's bytebuffer purpose:

byte[] bytes = bytebuffer.allocate(4).putint(1695609641).array();  (byte b : bytes) {     system.out.println(byte.tounsignedint(b)); } 

hope work ;)


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -