有關byte和int、long等等相關轉換
阿新 • • 發佈:2018-11-02
1、long與byte之間的轉換
public class Utilities { public static byte[] int2Bytes(int num) { byte[] byteNum = new byte[4]; for (int ix = 0; ix < 4; ++ix) { int offset = 32 - (ix + 1) * 8; byteNum[ix] = (byte) ((num >> offset) & 0xff); } return byteNum; } public static int bytes2Int(byte[] byteNum) { int num = 0; for (int ix = 0; ix < 4; ++ix) { num <<= 8; num |= (byteNum[ix] & 0xff); } return num; } public static byte int2OneByte(int num) { return (byte) (num & 0x000000ff); } public static int oneByte2Int(byte byteNum) { return byteNum > 0 ? byteNum : (128 + (128 + byteNum)); } public static byte[] long2Bytes(long num) { byte[] byteNum = new byte[8]; for (int ix = 0; ix < 8; ++ix) { int offset = 64 - (ix + 1) * 8; byteNum[ix] = (byte) ((num >> offset) & 0xff); } return byteNum; } public static long bytes2Long(byte[] byteNum) { long num = 0; for (int ix = 0; ix < 8; ++ix) { num <<= 8; num |= (byteNum[ix] & 0xff); } return num; } public static void main(String[] args) { int num = 129; System.out.println("測試的int值為:" + num); byte[] int2bytes = Utilities.int2Bytes(num); System.out.printf("int轉成bytes: "); for (int i = 0; i < 4; ++i) { System.out.print(int2bytes[i] + " "); } System.out.println(); int bytes2int = Utilities.bytes2Int(int2bytes); System.out.println("bytes轉行成int: " + bytes2int); byte int2OneByte = Utilities.int2OneByte(num); System.out.println("int轉行成one byte: " + int2OneByte); int oneByte2Int = Utilities.oneByte2Int(int2OneByte); System.out.println("one byte轉行成int: " + oneByte2Int); System.out.println(); long longNum = 100000; System.out.println("測試的long值為:" + longNum); byte[] long2Bytes = Utilities.long2Bytes(longNum); System.out.printf("long轉行成bytes: "); for (int ix = 0; ix < long2Bytes.length; ++ix) { System.out.print(long2Bytes[ix] + " "); } System.out.println(); long bytes2Long = Utilities.bytes2Long(long2Bytes); System.out.println("bytes轉行成long: " + bytes2Long); } }
2、int和byte轉換
package com.chargedot.util; /** * @Author:caoj * @Description: * @Data:Created in 2018/5/14 */ public class ByteUtil { public static int bytesToInt(byte[] data, int pos, int len) { int value = 0; for (int i = len - 1; i >= 0; i--) { int shift = i * 8; value += (data[pos + i] & 0x000000FF) << shift; } return value; } public static int byte2int(byte[] res) { int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) // | 表示安位或 | ((res[2] << 24) >>> 8) | (res[3] << 24); return targets; } public static void fillBytes(byte[] dst, int pos, int value, int len) { for (int i = 0; i < len; i++) { dst[pos + i] = (byte) (value & 0xff); value = (value >> 8); } } public static void fillBytes(byte[] dst, int pos, String value) { for (int i = 0; i < value.length(); i++) { dst[pos + i] = (byte) (value.charAt(i) & 0xff); } } public static byte[] intToBytes(int value) { byte[] b = new byte[4]; b[0] = (byte) (value & 0xff); b[1] = (byte) (value >> 8 & 0xff); b[2] = (byte) (value >> 16 & 0xff); b[3] = (byte) (value >> 24 & 0xff); return b; } public static byte[] intToBytes3(int value) { byte[] b = new byte[3]; b[0] = (byte) (value & 0xff); b[1] = (byte) (value >> 8 & 0xff); b[2] = (byte) (value >> 16 & 0xff); return b; } public static byte[] toHH2(int n) { byte[] b = new byte[2]; b[0] = (byte) (n & 0xff); b[1] = (byte) (n >> 8 & 0xff); return b; } //10進位制轉16進位制字串 public static String decimalToHex(int decimal) { String hex = ""; while(decimal != 0) { int hexValue = decimal % 16; hex = toHexChar(hexValue) + hex; decimal = decimal / 16; } return hex; } //將0~15的十進位制數轉換成0~F的十六進位制數 public static char toHexChar(int hexValue) { if(hexValue <= 9 && hexValue >= 0) return (char)(hexValue + '0'); else return (char)(hexValue - 10 + 'A'); } }