H
hierholzer
I'm converting an array of char to an array of bytes:
static public byte[] convertCharArrayToByteArray(char[] ca) {
byte[] ba = new byte[ca.length*2];
int j = 0;
byte mask = 0xff;
for(int i = 0; i < ca.length; ++i, j+=2) {
byte upper8bits = ((byte)(ca >> (1<<3)) & mask);
byte lower8bits = ((byte) ca & mask);
ba[j] = upper8bits;
ba[j+1] = lower8bits;
}
return ba;
}
I'm getting loss of precision because the primitive type byte is
represented
as signed 2s complement. hence, 0xff causes loss of precision issues
as with the other bit manipulation statements.
What is the suggested way around this in Java?
static public byte[] convertCharArrayToByteArray(char[] ca) {
byte[] ba = new byte[ca.length*2];
int j = 0;
byte mask = 0xff;
for(int i = 0; i < ca.length; ++i, j+=2) {
byte upper8bits = ((byte)(ca >> (1<<3)) & mask);
byte lower8bits = ((byte) ca & mask);
ba[j] = upper8bits;
ba[j+1] = lower8bits;
}
return ba;
}
I'm getting loss of precision because the primitive type byte is
represented
as signed 2s complement. hence, 0xff causes loss of precision issues
as with the other bit manipulation statements.
What is the suggested way around this in Java?