我正在用 Android 开发一个软件。在软件的特定部分,我需要将短格式转换为字节并重新转换为短格式。我试过下面的代码,但转换后值不一样。
short n, n1;
byte b1, b2;
n = 1200;
// short to bytes conversion
b1 = (byte)(n & 0x00ff);
b2 = (byte)((n >> 8) & 0x00ff);
// bytes to short conversion
short n1 = (short)((short)(b1) | (short)(b2 << 8));
执行代码后n和n1的值不一样。为什么?
最佳答案
我没有让 Grahams 解决方案起作用。然而,这确实有效:
n1 = (short)((b1 & 0xFF) | b2<<8);
关于java - Android 中短到字节和字节到短的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8533607/