(byte & 0xff)操作
先看一段代码:
@Test
public void test(){
byte a = -5;
byte b = 12;
System.out.println(a);
System.out.println(Integer.toHexString(a));
System.out.println(Integer.toHexString(a & 0xff));
System.out.println(b);
System.out.println(Integer.toHexString(b));
System.out.println(Integer.toHexString(b & 0xff));
}
执行结果:
-5
fffffffb
fb
12
c
c
解释:
1.负数在计算机中以补码形式保存,所以-5的二进制表示为11111011(负数补码的计算方式:绝对值的反码+1)
2.byte转换为int时,左边的24位补符号位,对于-5,转换后的二进制表示为11111111111111111111111111111011,这与之前的11111011,十进制都表示为-5,所以输出为-5
3.0xff为一个int整数,255,二进制表示为00000000000000000000000011111111,-5 & 0xff,即将-5的高24位置0
4.Integer.toHexString函数,循环右移,每次取4位,转换为16进制字符串,所以11111111111111111111111111111011转成16进制字符串,前面出现多次的'1111'都转成了'f'
Integer.toHexString源码如下:
/**
* Convert the integer to an unsigned number.
*/
//shift=4
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0); return new String(buf, charPos, (32 - charPos));
} /**
* All possible chars for representing a number as a String
*/
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
(byte & 0xff)操作的更多相关文章
- byte & 0xff char 转换
https://blog.csdn.net/lixingtao0520/article/details/75450883 版权声明:本文为博主原创文章,转载请注明作者与出处,http://blog.c ...
- System.Buffer 以字节数组(Byte[])操作基元类型数据
1. Buffer.ByteLength:计算基元类型数组累计有多少字节组成. 该方法结果等于"基元类型字节长度 * 数组长度" , , }; , , }; , , }; Cons ...
- 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 觉得有用的话,欢迎一起讨论相互学习~Follow Me 今 ...
- python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position
python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 实用python的时候 打开一个csv的文件出 ...
- TensorFlow学习笔记(UTF-8 问题解决 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte)
我使用VS2013 Python3.5 TensorFlow 1.3 的开发环境 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff ...
- tensorflow UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
tensorflow读取图像出现错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid s ...
- TensorFlow学习('utf-8' codec can't decode byte 0xff in position 0: invalid start byte)
使用语句: image_raw_data = tf.gfile.GFile("./picture.jpg", "r").read() 读取图像时报错如下: Un ...
- Java byte位移操作 注意事项
Java对byte 的 + - * / >> >>> << & | ^ (加,减,乘,除,右移,左移,无符号右移,位与,位或,位异或)操作,均会是首先 ...
- Java byte 位移操作 注意事项
转自:http://blog.163.com/pilgrim_yang/blog/static/55631481201111542151582/ Java对byte 的 + - * / >> ...
随机推荐
- pikachs 渗透测试2-XSS漏洞及利用
一.概述 XSS(跨站脚本)概述 Cross-Site Scripting 简称为"CSS",为避免与前端叠成样式表的缩写"CSS"冲突,故又称XSS.一般XS ...
- html图片拖放
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- 面试半年,凭借这份JVM面试题,我终于拿到了字节跳动的offer!
内存区域 虚拟机栈生命周期与线程相同,描述的是Java 方法执行的内存模型,每个方法在执行的时候都会创建一个栈帧,用于存取局部变量表.操作数栈.动态链接.方法出口等信息本地方法栈与虚拟机栈作用相似,只 ...
- go-zero 如何扛住流量冲击(二)
本篇文章承接上一篇go-zero 如何扛住流量冲击(一). 上一篇介绍的是 go-zero 中滑动窗口限流,本篇介绍另外一个 tokenlimit ,令牌桶限流. 使用 const ( burst = ...
- pip更新报错问题
pip更新错误如下: WARNING: You are using pip version 20.1.1; however, version 20.2 is available. You should ...
- python3 Redis未授权检测脚本
`import sys import getopt import socket def get_target(): opts, args = getopt.getopt(sys.argv[1:], ' ...
- 1. git简介
1.1 版本控制理解 版本控制 工程设计领域中使用版本控制管理工程蓝图的设计过程,在 IT 开发过程中也可以使用版本控制思想管理代码的版本迭代 集中式版本控制工具 CVS.SVN.VSS等 分布式版本 ...
- sqli-labs-master less01
注:如未接触过sql注入,建议观看前期知识点文章 https://www.cnblogs.com/yyd-sun/p/12256407.html 第一关步骤 一.判断注入类型(数字/字符) (1).h ...
- eNSP VLAN划分基础配置及Trunk接口
跨交换机实现VLAN通信拓扑图: 一.配置PC机 ip 并测试相互能否ping通 PC名称 IP 子网掩码 网关 PC1 10.1.1.1 255.255.255.0 10.1.1.254 PC2 1 ...
- CentOS下搭建文件共享服务
nfs部署以及优化 Server端配置 安装rpm服务包 yum install -y nfs-utils 创建数据挂载点 mkdir -p /data 编辑exports文件 vi /etc/exp ...