Android为TV端助力 进制互相转换

byte转换为16进制

public static String GetByte2Str(byte b) {

byte[] buff = new byte[2];

 buff[0] = mHex[(b >> 4) & 0x0f];

buff[1] = mHex[b & 0x0f];

return new String(buff);

}

private static final byte[] mHex = "0123456789ABCDEF".getBytes();

16进制转换为10进制

int integerState = Integer.valueOf(GetByte2Str(mAcState), 16);

10进制转换为2进制串

String s=Integer.toBinaryString(integerState); //二进制串

2进制的字符串转换为10进制

String aa = "1010";
Integer f = Integer.parseInt(aa,2);

Java中在声明数字时默认采用的是十进制,可以在数字前加上符号表示数字采用八进制【前面加0(零)】或者十六进制【前面加上0x(零x)】。

 Java的整型封装类Integer和Long提供toString(int i,int radix)静态方法,可以将一个任意进制的整数转换为其他进制的整数。

 使用Integer或Long的toBinaryString方法将整数转换为二进制。

使用Integer或Long的toOctalString方法将整数转换为八进制。

使用Integer或Long的toHexString方法将整数转换为十六进制。

使用Integer或Long的toString(int i)方法可以将其他进制的整数转换为十进制的整数的字符串表示。

原文地址:https://www.cnblogs.com/xiaoxiaing/p/7422158.html