android 进制转换 (高低位)

【android进制转换】
https://www.yiibai.com/java/lang/integer_highestonebit.html

android中在声明数字时默认采用的是十进制。

 //十进制转成十六进制  Integer.toHexString(14);
 //十进制转成八进制    Integer.toOctalString(14);
 //十进制转成二进制    Integer.toBinaryString(12);

 //十六进制转成十进制  Integer.valueOf("FFFF",16).toString();
 //十六进制转成二进制   Integer.toBinaryString(Integer.valueOf("FFFF",16));
 //十六进制转成八进制  Integer.toOctalString(Integer.valueOf("FFFF",16));

 //八进制转成十进制    Integer.valueOf("576",8).toString();
 //八进制转成二进制    Integer.toBinaryString(Integer.valueOf("23",8));
 //八进制转成十六进制  Integer.toHexString(Integer.valueOf("23",8));

 //二进制转十进制      Integer.valueOf("0101",2).toString();
 //二进制转八进制        Integer.toOctalString(Integer.parseInt("0101", 2));
 //二进制转十六进制    Integer.toHexString(Integer.parseInt("0101", 2));


SOCKET通讯:int32 int64 long
byte[] buffer=new byte[1024];
接收字节数组:index=in.read(buffer,0,buffer.length);

【高端在前】

//字节数组转int型,高位在前,低位在后  (int 4字节)=(pc端int32默认高位在前)
public static int bytes4ToInt(byte[] src) {
    int value = (int) (
    ((src[0] & 0xFF)<<24)|
               ((src[1] & 0xFF)<<16)|
               ((src[2] & 0xFF)<< 8)|
                (src[3] & 0xFF));
    return value;
}
//int型转byte[],高位在前,低位在后  (int 4字节)=(pc端int32默认高位在前)
public static byte[] intToBytes4H(int res){
    byte[] targets = new byte[4];
    targets[0] = (byte) (res  >>24 & 0xff);
    targets[1] = (byte) ((res >>16) & 0xff);
    targets[2] = (byte) ((res >> 8) & 0xff);
    targets[3] = (byte) (res & 0xff);
    return targets;
}
//字节数组转long型,高位在前,低位在后  (long 8字节)=(pc端int64默认高位在前)
public static long  byte8TolongH( byte[] buffer ) {
 long num= ((buffer[0]&0xff) << 56) +
            ((buffer[1]&0xff) <<48) +
            ((buffer[2]&0xff) <<40) +
            ((buffer[3]&0xff)<<32) +
            ((buffer[4]&0xff)<<24)+
            ((buffer[5]&0xff)<<16) +
            ((buffer[6]&0xff)<<8) +
            (buffer[7]& 0xff);
    return num;
}
//long型转byte[],高位在前,低位在后  (long 8字节)=(pc端int64默认高位在前)
public static byte[] longTobyte8H(long res) {
    byte[] targets = new byte[8];
    targets[0] = (byte) (res >>> 56 & 0xff);
    targets[1] = (byte) (res >>> 48 & 0xff);
    targets[2] = (byte) (res >>> 40 & 0xff);
    targets[3] = (byte) (res >>> 32 & 0xff);
    targets[4] = (byte) (res >>> 24 & 0xff);
    targets[5] = (byte) ((res >> 16)& 0xff);
    targets[6] = (byte) ((res >> 8) & 0xff);
    targets[7] = (byte) (res & 0xff);
    return targets;
}

【低端在前】

//字节数组转int型,低位在前,高位在后  (int 4字节)=(pc端int32默认高位在前)
public static int bytes4ToInt(byte[] src) {
    int value = (int) (
    ((src[0] & 0xFF))|
               ((src[1] & 0xFF)<< 8)|
               ((src[2] & 0xFF)<<16)|
                (src[3] & 0xFF)<<24);
    return value;
}
//int型转byte[],低位在前,高位在后  (int 4字节)=(pc端int32默认高位在前)
public static byte[] intToBytes4L(int res){
    byte[] targets = new byte[4];
    targets[0] = (byte) (res & 0xff);
    targets[1] = (byte) ((res >> 8) & 0xff);
    targets[2] = (byte) ((res >>16) & 0xff);
    targets[3] = (byte) ((res >>24) & 0xff);
    return targets;
}
//字节数组转long型,低位在前,高位在后  (long 8字节)=(pc端int64默认高位在前)
public static long  byte8TolongL( byte[] buffer ) {
   long num=  (buffer[0]& 0xff) +
              ((buffer[1]&0xff) <<8) +
              ((buffer[2]&0xff) <<16) +
              ((buffer[3]&0xff) <<24)+
              ((buffer[4]&0xff) <<32) +
              ((buffer[5]&0xff) <<40) +
              ((buffer[6]&0xff) <<48) +
              ((buffer[7]&0xff) << 56);
  return num;
}
//long型转byte[],低位在前,高位在后  (long 8字节)=(pc端int64默认高位在前)
public static byte[] longTobyte8L(long res) {
byte[] targets = new byte[8];
targets[0] = (byte) (res & 0xff);
targets[1] = (byte) ((res >> 8) & 0xff);
targets[2] = (byte) ((res >> 16)& 0xff);
targets[3] = (byte) (res >>> 24 & 0xff);
targets[4] = (byte) (res >>> 32 & 0xff);
targets[5] = (byte) (res >>> 40 & 0xff);
targets[6] = (byte) (res >>> 48 & 0xff);
targets[7] = (byte) (res >>> 56 & 0xff);
   return targets;
}
【Byte[] ---- HexString】如串口通信

//byte[]转换成hexString字符串,无符号。每个Byte之间无空格分隔。字母小写
public static String Bytes2HexString(byte[] b, int size){
//size==b.length,toUpperCase()将字符串小写字符转换为大写,toLowerCase()将字符串大写字符转换为小写
     String ret = "";
     for (int i = 0; i < size; i++) {
       String hex = Integer.toHexString(b[i] & 0xFF);
       if (hex.length() == 1) {
    hex = "0" + hex;
   }
       ret += hex.toUpperCase();
     }
     return ret;
   }

//HexString字符串转换成byte[]无符号。每个Byte之间无空格分隔。
public static byte[] HexString2Bytes(String src) {
 int len = src.length() / 2;
 byte[] ret = new byte[len];
 byte[] tmp = src.getBytes();
 for (int i = 0; i < len; i++) {
     ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
 }
 return ret;
 }
public static byte uniteBytes(byte src0, byte src1) {
 byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
 _b0 = (byte)(_b0 << 4);
 byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
 byte ret = (byte)(_b0 ^ _b1);
 return ret;
}


【String ---- HexString】

//String字符串转换成hexString字符串,无符号。每个Byte之间无空格分隔。如“name”
public static String str2HexStr(String str) {
 char[] chars = "0123456789ABCDEF".toCharArray();
 StringBuilder sb = new StringBuilder("");
 byte[] bs = str.getBytes();
 int bit;
 for (int i = 0; i < bs.length; i++) {
  bit = (bs[i] & 0x0f0) >> 4;
  sb.append(chars[bit]);
  bit = bs[i] & 0x0f;
  sb.append(chars[bit]);
  //sb.append(' ');//有符号,每个Byte之间用空格分隔
 }
 return sb.toString().trim();
}
//无符号hexString字符串,转换成String字符串。每个Byte之间无空格分隔。
public static String hexStr2Str(String hexStr) {
 String info="";
 String str = "0123456789ABCDEF";
 char[] hexs = hexStr.toCharArray();
 byte[] bytes = new byte[hexStr.length() / 2];
 int n;
 for (int i = 0; i < bytes.length; i++) {
  n = str.indexOf(hexs[2 * i]) * 16;
  n += str.indexOf(hexs[2 * i + 1]);
  bytes[i] = (byte) (n & 0xff);
 }
 return new String(bytes);
}

你可能感兴趣的:(android)