java - Concatenate bytes to get char -
how 1 concatenate 2 byte primitives 1 char in java? i'm trying this:
byte = 0x01; byte b = 0x02; char c = (char) (a + b);
which gives 0x03. answer want 0x0102.
is there no simple way concatenate primitives? i'm surprised there isn't obvious solution, since seems should easy. maybe there , don't see it. :p
any appreciated. thanks!
by shifting left 8 bits , adding. like,
byte = 0x01; byte b = 0x02; char c = (char) ((a << 8) + b); system.out.println(integer.tohexstring(c));
output is
102
as pointed out in comments (by @chrismartin) bitwise-or (and i'll add might xor
) like
char c = (char) ((a << 8) | b);
and
char c = (char) ((a << 8) ^ b);
to achieve same result. since should write dumb code suggest use whichever find easiest understand.
Comments
Post a Comment