byte ---> hex String

public static String byte2HexString(byte[] b){
        String ret = "";
        for(int i=0;i<b.lenght;i++){
                   String hex = Integer.toHexString(b[i]&0XFF);
                   if(hex.length()==1){
                               hex = '0'+hex;
                   }
                       ret+=hex.toUpperCase();
    }
               return ret;
}    

1  1个字节8位          1个BYTE与上2个hex字符
  1 个HEX字符4位

2 Integer.toHexString(int ---);

作用 int(十进制有符号)  ---> String (十六进制)

3 byte ==>int

byte b;

int i=b&0Xff;

 

分析 :byte -->8 位 int 32位

byte转int位数不够要补位 补位有偏差 故需要清零多出的高24位 即与上0xFF

补码:

   1111 1111

  [ 1111 1111 ] [ 1111 1111 ] [ 1111 1111 ] [1111 1111]

   0XFFFF FFFF与上0Xff高24位清零

 

&1&1 -->1

1&0 -->0

0&1 -->0

0&0 --->0

两边都成立才可以为真


1|1 -->1
1|0 -->1
0|1 -->1
0|0 -->0
两边只有一方成立即可

byte b = 24;

int i = b&FF; 清零高24位

 

-2  

字节       1111 1110 补码

字 1111 1111  1111 1110 补码

 

字节       1000 0010 源码

字   0000 0000  1000 0010 源码

            二进制      十进制

1B byte = 8 bit    1B = 8bit    1B=8bit

1 KB =1024B     1KB= 2^10B   1KB = 10^3B 

1MB =1024kb      1MB = 2^20B  1MB=10^6b

1GB = 1024mb    1GB = 2^30B    1GB = 10^9B

1TB = 1024GB    1TB= 2^40B      1TB= 10^12B

 

1024          2^10      10^3

 

hex string  == byte

1 hex string  -- integer    Integer i = Integer.parseInt("EA",16);

2 integer -- byte        Byte b = i.byteValue();

Byte HexString        byte b = -22;

1 byte -->integer        Integer i = b.intValue();

2 integer -->string       String newII = Integer.toHexString(ii).subString(6,8);

转载于:https://www.cnblogs.com/cici-new/p/4298706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值