- 浏览: 1340990 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (551)
- 计划 (4)
- java (115)
- oracle (60)
- ajax (3)
- javascript (64)
- 计算机操作技巧集 (11)
- 近期关注话题 (10)
- 随想 (13)
- html (6)
- struts (15)
- hibernate (16)
- spring (2)
- game (0)
- Eglish (10)
- DisplayTag (6)
- jsp (18)
- css (3)
- eclipse (3)
- 其他知识 (8)
- 备用1 (12)
- 备用2 (1)
- 笑话-放松心情 (9)
- 设计 (1)
- 设计模式 (1)
- 数据结构 (0)
- office办公软件 (5)
- webwork (0)
- tomcat (2)
- MySql (1)
- 我的链接资源 (5)
- xml (2)
- servlet (0)
- PHP (13)
- DOM (0)
- 网页画图vml,canvas (1)
- 协议 (2)
- 健康 (3)
- 书籍下载 (1)
- jbpm (1)
- EXT (1)
- 自考 (2)
- 报表 (4)
- 生活 (64)
- 操作系统基础知识 (2)
- 测试 (2)
- guice (1)
- google学习 (2)
- Erlang (1)
- LOG4J (2)
- wicket (1)
- 考研 (1)
- 法律 (1)
- 地震 (1)
- 易学-等等相关 (1)
- 音乐 (1)
- 建站 (4)
- 分享说 (3)
- 购物省钱 (0)
- linux (1)
最新评论
-
zenmshuo:
如果使用SpreadJS这一类的表格工具,应该能更好的实现这些 ...
js中excel的用法 -
hjhj2991708:
第一个已经使用不了
jar包查询网站 非常好用! -
jiangmeiwei:
...
中文乱码 我的总结 不断更新 -
gary_bu:
...
response.sendRedirect 中文乱码问题解决 -
hnez:
多谢指点,怎么调试也不通,原来我在<body>&l ...
ExtJs IE ownerDocument.createRange() 错误解决方案
Although data is stored in the array as signed Java bytes with values between -128 and 127, there's a simple one-to-one correspondence between these signed values and the unsigned bytes normally used in I/O. This correspondence is given by the following formula:
Since bytes have such a small range, they're often converted to ints in calculations and method invocations. Often, they need to be converted back, generally through a cast. Therefore, it's useful to have a good grasp of exactly how the conversion occurs.
Casting from an int to a bytefor that matter, casting from any wider integer type to a narrower typetakes place through truncation of the high-order bytes. This means that as long as the value of the wider type can be expressed in the narrower type, the value is not changed. The int 127 cast to a byte still retains the value 127.
On the other hand, if the int value is too large for a byte, strange things happen. The int 128 cast to a byte is not 127, the nearest byte value. Instead, it is -128. This occurs through the wonders of two's complement arithmetic. Written in hexadecimal, 128 is 0x00000080. When that int is cast to a byte, the leading zeros are truncated, leaving 0x80. In binary, this can be written as 10000000. If this were an unsigned number, 10000000 would be 128 and all would be fine, but this isn't an unsigned number. Instead, the leading bit is a sign bit, and that 1 does not indicate 27 but a minus sign. The absolute value of a negative number is found by taking the complement (changing all the 1 bits to 0 bits and vice versa) and adding 1. The complement of 10000000 is 01111111. Adding 1, you have 01111111 + 1 = 10000000 = 128 (decimal). Therefore, the byte 0x80 actually represents -128. Similar calculations show that the int 129 is cast to the byte -127, the int 130 is cast to the byte -126, the int 131 is cast to the byte -125, and so on. This continues through the int 255, which is cast to the byte -1.
in Java source code, all numbers preceded by 0x are read as hexadecimal.
When 256 is reached, the low-order bytes of the int are filled with zeros. In other words, 256 is 0x00000100. Thus, casting it to a byte produces 0, and the cycle starts over. This behavior can be reproduced algorithmically with this formula, though a cast is obviously simpler:
int unsignedByte = signedByte >= 0 ? signedByte : 256 + signedByte;
Since bytes have such a small range, they're often converted to ints in calculations and method invocations. Often, they need to be converted back, generally through a cast. Therefore, it's useful to have a good grasp of exactly how the conversion occurs.
Casting from an int to a bytefor that matter, casting from any wider integer type to a narrower typetakes place through truncation of the high-order bytes. This means that as long as the value of the wider type can be expressed in the narrower type, the value is not changed. The int 127 cast to a byte still retains the value 127.
On the other hand, if the int value is too large for a byte, strange things happen. The int 128 cast to a byte is not 127, the nearest byte value. Instead, it is -128. This occurs through the wonders of two's complement arithmetic. Written in hexadecimal, 128 is 0x00000080. When that int is cast to a byte, the leading zeros are truncated, leaving 0x80. In binary, this can be written as 10000000. If this were an unsigned number, 10000000 would be 128 and all would be fine, but this isn't an unsigned number. Instead, the leading bit is a sign bit, and that 1 does not indicate 27 but a minus sign. The absolute value of a negative number is found by taking the complement (changing all the 1 bits to 0 bits and vice versa) and adding 1. The complement of 10000000 is 01111111. Adding 1, you have 01111111 + 1 = 10000000 = 128 (decimal). Therefore, the byte 0x80 actually represents -128. Similar calculations show that the int 129 is cast to the byte -127, the int 130 is cast to the byte -126, the int 131 is cast to the byte -125, and so on. This continues through the int 255, which is cast to the byte -1.
in Java source code, all numbers preceded by 0x are read as hexadecimal.
When 256 is reached, the low-order bytes of the int are filled with zeros. In other words, 256 is 0x00000100. Thus, casting it to a byte produces 0, and the cycle starts over. This behavior can be reproduced algorithmically with this formula, though a cast is obviously simpler:
int byteValue; int temp = intValue % 256; if ( intValue < 0) { byteValue = temp < -128 ? 256 + temp : temp; } else { byteValue = temp > 127 ? temp - 256 : temp; }
评论
3 楼
twjava
2008-11-04
int byteValue = intValue & 0xff;
2 楼
jerypi
2008-06-26
1 楼
jerypi
2008-06-26
发表评论
-
TilesRequestProcessor - Tiles definition factory found for request processor ''
2011-11-03 15:39 2416TilesRequestProcessor - T ... -
jar包查询网站 非常好用!
2009-12-28 11:47 13332java jar包查询 根据类查询所在的java包 http ... -
tomcat 启动错误 java.lang.UnsupportedClassVersionError: Bad version number in .class
2009-11-27 08:53 2222请访问http://ddgrow.com/bad-versio ... -
arrayToString
2009-04-07 09:01 1390原文:http://leepoint.net/notes-ja ... -
native2ascii
2009-04-02 12:00 1139D:\>native2ascii 中国 \u4e2d\u ... -
java swing 架构
2009-03-04 15:03 2274下面是偶尔找到的一篇 相当好的文章 ,针对 java swin ... -
红帽企业5序列号
2009-02-27 09:11 2497这是从[url]http://www.21codes.com/ ... -
中文乱码 我的总结 不断更新
2009-02-23 09:51 2240java中文乱码问题 1、jsp中文乱码 <0& ... -
websphere 数据库连接
2009-02-19 17:47 2069问个问题: 应用服务器: websphere 连接池:pr ... -
打开java控制台的方法
2009-02-10 17:10 2995D:\Program Files\Java\jre6\bin\ ... -
log4j weblogic 问题
2009-01-21 15:08 1883问题是这样的 最初在tomcat5.5下面开发,使用了log ... -
log4j weblogic 问题
2009-01-21 15:07 0问题是这样的 最初在tomcat5.5下面开发,使用了log ... -
java.lang.NoClassDefFoundError in quartz
2008-12-24 15:35 2851quartz-1.5.2.jar tomcat5.5 当使 ... -
java 得到运行时系统中的内存信息
2008-12-19 20:54 1466这个在tomcat下面使用过,在引记录下来,以后方便查找 Ru ... -
log4j
2008-12-13 11:32 859讲解了用xml配置,还有例子,以及对 levelmin lev ... -
servlet 2.3 规范
2008-12-02 20:55 0servlet 2.3 规范 http://www.orio ... -
session 问题
2008-11-26 16:41 936问个问题,如下: 我要在一个A系统中,通过一个弹出窗口,超链接 ... -
java 反编译工具
2008-10-08 11:03 1955从哪找到的给忘了,用起来不错,放在这里,以后找起来方便 ,有需 ... -
thinking in java 读书笔记
2008-10-06 22:09 1300记录下来,以便以后查看 thinking in java 4t ... -
RSA 算法 java版本<转>
2008-09-23 18:28 2389原文地址:http://www.cs.princeton.ed ...
相关推荐
char short int long float double 转换成byte数组
Java bytes数组与基本类型的相互转换 Int -> Bytes int64ToByte8 int48ToByte6 int40ToByte5 int32ToByte4 int24ToByte3 int16ToByte2 int8ToByte Bytes -> Int bytesToInt64 bytesToInt48 bytesToInt40 bytesTo...
ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...
C# Byte数组 数值转花 Int32 Short Float 浮点数 自己写的Test 有问题可以联系我
TIA博途_字节Byte转换成双字Dword全局FC库文件_GF_Byte_To_Dword
LabVIEW程序,功能:将4字节的unsigned char输入组合成1个32-bit int值,若输入字节数不等于4则报错。
byte转化为int有两种情况: 1)要保持数值不变 应用场景:数值计算。等等。 方法:能够直接?用强制类型转换:int i = (int) aByte, 比如:若aByte=0xff(即数值为-1)。则转化为int后。i为0xffffffff。...
写一个方法,将int类型转换为字节数组,输入任意int类型整型,输出字节数组;写第二个方法,输入字节数组,输出对应int类型数据。
下面小编就为大家带来一篇基于java中byte数组与int类型的转换(两种方法)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
第一反应是想起书本上的理论,无符号数表示范围是多少到多少,有符号数的表示范围是多少到多少,这就把问题复杂化了, 别忘了uint类型: uint raw = (uint)(bytes[7] * 256 + bytes[8]); //本项目的实际情况,用两...
TIA博途_S7-1200中将BYTE类型的数据转换成char类型的具体方法和注意事项
java字节流相关操作,int转换成byte字节数组,String转换为byte字节数组,bcd码转换。
下面小编就为大家带来一篇Java 字节数组类型(byte[])与int类型互转方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
介绍php字符串与byte字节数组转化类,转换一个String字符串为byte数组,将字节数组转化为String类型的数据,转换一个int为byte数组,从字节数组中指定的位置读取一个Integer类型的数据,转换一个shor字符串为byte...
分别给大家介绍了Java中byte和int之间的转换、Java中 byte数组和int之间的转换、Java中byte数组和long之间的转换以及整理了整体工具类的源码,需要的朋友可以参考借鉴,下面来一起看看吧。
Java字节数组与常见类型转换工具,包括byte[] 与int long double float string boolean 等类型的相互转换。
* int : 整数类型 占4个字节 32位二进制 byte[0] byte[1] byte[2] byte[3] * long: 长整数类型 占8个字节 64位二进制 byte[0] byte[1] byte[2] byte[3] byte[4] byte[5] * byte[6] byte[7] * float: ...
将整型值类型转换为字节类型将int类型转换为byte类型
觉得有用,记得点赞 对于小于零的数字或者字节,转换时... 有符号:True 无符号:False 如: >>>(1024).to_bytes(2,'big') b'\x04\x00' >>>(-1024).to_bytes(2,'big') Traceback (most recent call last): File ,
这个类库可以实现 1.转换一个String字符串为...3.转换一个int为byte数组 4.从字节数组中指定的位置读取一个Integer类型的数据 5.转换一个shor字符串为byte数组 6.从字节数组中指定的位置读取一个Short类型的数据