int与byte、byte[]相互转换

深藏阁楼爱情的钟 2024-04-17 19:33 153阅读 0赞

一、int to byte

范围在 -128 ~ 127之间可以直接转换,超出这个范围就乱了。

  1. int i = 127;
  2. byte b = (byte) i;
  3. Integer i = 127;
  4. i.byteValue();

二、byte to int

  1. // byte to int (int仍有正负)
  2. static int byte2Int(byte byt){
  3. // 直接强转
  4. return (int) byt;
  5. }
  6. // byte to int (int无符号)
  7. static int byte2UnsignedInt(byte byt){
  8. /* 与强转的区别:强转之后表示数值不变(不论正数、0、负数),如-120强转后还是-120, ************* 但是这种方式强转之后,正数和0也不变,但是负数会变成:输入值+2<sup>8</sup>,如 -120 转换后变为136,即-120+256 */
  9. return Byte.toUnsignedInt(byt);
  10. //return ((int) byt) & 0xFF; // Byte.toUnsignedInt(byt)就是这样实现的
  11. }

三、int to byte[]

方式一

低位在前高位在后
  1. /** * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用 * @param value * 要转换的int值 * @return byte数组 */
  2. public static byte[] intToBytes( int value )
  3. {
  4. byte[] src = new byte[4];
  5. src[3] = (byte) ((value>>24) & 0xFF);
  6. src[2] = (byte) ((value>>16) & 0xFF);
  7. src[1] = (byte) ((value>>8) & 0xFF);
  8. src[0] = (byte) (value & 0xFF);
  9. return src;
  10. }
高位在前低位在后
  1. /** * 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。 和bytesToInt2()配套使用 */
  2. public static byte[] intToBytes2(int value)
  3. {
  4. byte[] src = new byte[4];
  5. src[0] = (byte) ((value>>24) & 0xFF);
  6. src[1] = (byte) ((value>>16)& 0xFF);
  7. src[2] = (byte) ((value>>8)&0xFF);
  8. src[3] = (byte) (value & 0xFF);
  9. return src;
  10. }

方式二

低位在前高位在后
  1. /** * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 * @param value * 要转换的int值 * @return byte数组 */
  2. public static byte[] intToBytes(int value)
  3. {
  4. byte[] byte_src = new byte[4];
  5. byte_src[3] = (byte) ((value & 0xFF000000)>>24);
  6. byte_src[2] = (byte) ((value & 0x00FF0000)>>16);
  7. byte_src[1] = (byte) ((value & 0x0000FF00)>>8);
  8. byte_src[0] = (byte) ((value & 0x000000FF));
  9. return byte_src;
  10. }

三、byte[] to int

方式一

低位在前高位在后
  1. /** * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用 * * @param src * byte数组 * @param offset * 从数组的第offset位开始 * @return int数值 */
  2. public static int bytesToInt(byte[] src, int offset) {
  3. int value;
  4. value = (int) ((src[offset] & 0xFF)
  5. | ((src[offset+1] & 0xFF)<<8)
  6. | ((src[offset+2] & 0xFF)<<16)
  7. | ((src[offset+3] & 0xFF)<<24));
  8. return value;
  9. }
高位在前低位在后
  1. /** * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用 */
  2. public static int bytesToInt2(byte[] src, int offset) {
  3. int value;
  4. value = (int) ( ((src[offset] & 0xFF)<<24)
  5. |((src[offset+1] & 0xFF)<<16)
  6. |((src[offset+2] & 0xFF)<<8)
  7. |(src[offset+3] & 0xFF));
  8. return value;
  9. }

方式二

低位在前高位在后
  1. /** * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序。 * * @param ary * byte数组 * @param offset * 从数组的第offset位开始 * @return int数值 */
  2. public static int bytesToInt(byte[] ary, int offset) {
  3. int value;
  4. value = (int) ((ary[offset]&0xFF)
  5. | ((ary[offset+1]<<8) & 0xFF00)
  6. | ((ary[offset+2]<<16)& 0xFF0000)
  7. | ((ary[offset+3]<<24) & 0xFF000000));
  8. return value;
  9. }

参考:
byte与int强制转换
【转】java中byte, int的转换, byte String转换
byte[]数组和int之间的转换

发表评论

表情:
评论列表 (有 0 条评论,153人围观)

还没有评论,来说两句吧...

相关阅读