java —— int和byte的相互转化工具方法

浅浅的花香味﹌ 2024-02-19 19:13 128阅读 0赞

int转byte方法:

  1. public static byte[] intToByteArray(int i) {
  2. byte[] result = new byte[4];
  3. result[0] = (byte)((i >> 24) & 0xFF);
  4. result[1] = (byte)((i >> 16) & 0xFF);
  5. result[2] = (byte)((i >> 8) & 0xFF);
  6. result[3] = (byte)(i & 0xFF);
  7. return result;
  8. }

byte转int方法:

  1. public static int byteArrayToInt(byte[] bytes) {
  2. int value=0;
  3. for(int i = 0; i < 4; i++) {
  4. int shift= (3-i) * 8;
  5. value +=(bytes[i] & 0xFF) << shift;
  6. }
  7. return value;
  8. }

发表评论

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

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

相关阅读