求个进制转换方法解决方案
求个进制转换方法
将十进制的88转为16进制,然后放到byte数组中,入: byte[] bytes = new byte[]{0x00,0x00,0x00,0x58};
帮忙写个这样的算法,要求byte[]的大小为4位。
------解决方案--------------------
将十进制的88转为16进制,然后放到byte数组中,入: byte[] bytes = new byte[]{0x00,0x00,0x00,0x58};
帮忙写个这样的算法,要求byte[]的大小为4位。
------解决方案--------------------
- Java code
public static byte[] getByte(int a) { byte[] bs = new byte[4]; bs[0] = (byte)((a >> 24) & 0xff); bs[1] = (byte)((a >> 16) & 0xff); bs[2] = (byte)((a >> 8) & 0xff); bs[3] = (byte)((a) & 0xff); return bs; }
------解决方案--------------------
int i = 88;
String i16 = Integer.toHexString(i);
byte[] buf = i16.getBytes();
for(int j = 0;j < buf.length;j++){
System.out.println(buf[j]);
}
------解决方案--------------------
- Java code
public static byte[] getByte(int a) { byte[] bs = new byte[4]; bs[0] = (byte)((a >> 24) & 0xff); bs[1] = (byte)((a >> 16) & 0xff); bs[2] = (byte)((a >> 8) & 0xff); bs[3] = (byte)((a) & 0xff); return bs; }
------解决方案--------------------