`
zgqynx
  • 浏览: 1355664 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

有符号字节转成无符号字节 java int 转成 byte

    博客分类:
  • java
阅读更多
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:

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  
   
   
   
   
   

相关推荐

    java byte相互转换详解左右位移

    在Java中,byte是一个8位的有符号整型数据,它的取值范围是-128到127。int是一个32位的有符号整型数据,取值范围是-2,147,483,648到2,147,483,647。由于byte与int的数据位数不同,转换时需要进行特定的处理。 **...

    JAVA转byte[]为int,long,double

    以上就是关于“JAVA转byte[]为int,long,double”的知识讲解,理解这些转换对于处理二进制数据至关重要,尤其是在网络通信、序列化和反序列化等场景中。通过熟练掌握这些转换,你可以更好地处理和解析不同数据类型的...

    C# Byte数组转Int32 Short Float(浮点数)

    标题和描述提到的“C# Byte数组转Int32 Short Float(浮点数)”是指将字节数组中的数据转换为整型(Int32)、短整型(Short)以及浮点数(Float)的过程。以下是对这个主题的详细解释: **字节数组基础** 字节数组...

    java实现的字节数组转换成基本类型,基本类型转换成byte[]

    char short int long float double 转换成byte数组

    java基本类型与byte数组互相转换.pdf

    字符类型在 Java 中是 16 位的 Unicode 字符,转换成 byte 数组需要将字符类型转换成 int 类型,然后将 int 类型转换成 byte 数组。例如: ```java public static byte[] charToByteArr(char ch) { byte[] b = new...

    andriod byte 转int,string,数组,互转

    byte转化工具类,可以实现byte转int,数组,string,小端取高位,低位等

    Java数值类型与byte数组相互转换

    Java bytes数组与基本类型的相互转换 Int -&gt; Bytes int64ToByte8 int48ToByte6 int40ToByte5 int32ToByte4 int24ToByte3 int16ToByte2 int8ToByte Bytes -&gt; Int bytesToInt64 bytesToInt48 bytesToInt40 bytesTo...

    Java byte数组与其他类型转换

    本文将深入探讨如何在Java中将byte数组与其他基本类型(如short、int、long)进行转换,这些转换在处理二进制数据、网络通信或序列化等方面至关重要。 首先,我们来看byte数组与short之间的转换。在Java中,byte...

    java数据类型转byte数组

    ip地址转4字节byte,char转2字节byte,byte数组转char,int整数转换为4字节的byte数组,byte数组转换为int整数,double类型转8字节数组,8位数组转double,long整数转换为8字节的byte数组,short整数转换为2字节的...

    TIA博途-字节Byte转换成双字Dword全局FC库文件-V17版本-GF-Byte-To-Dword.zip

    GF_Byte_To_Dword函数块设计用于将多个字节数据合并成一个双字,这对于处理多字节数据如16位整数或特定的协议通信非常有用。 V17版本表明该库文件是针对TIA博途的一个较新版本编写的,这意味着它可能包含了对新功能...

    基于java中byte数组与int类型的转换(两种方法)

    `&` 是按位与操作符,`&gt;&gt;` 是有符号右移操作符,`&gt;&gt;&gt;` 是无符号右移操作符。通过这些操作,我们将`int`的每一位分别存储到`byte`数组的不同位置。 #### `byte`数组转`int` ```java public static int byte2int...

    在Java中int和byte[]的相互转换

    在Java编程语言中,有时我们需要将整型(int)数据与字节数组(byte[])之间进行转换,这在处理网络通信、序列化或存储数据时尤为常见。本文将深入探讨Java中int与byte[]的转换方法。 首先,让我们理解为什么需要进行...

    C# byte转为有符号整数实例

    要将这两个字节合并成一个有符号整数,我们可以首先将它们转换为`uint`(无符号32位整数),然后转换为`int`。这样做的原因是因为`uint`可以表示所有可能的`byte`组合,包括表示负数的情况。示例如下: ```csharp ...

    LabVIEW,字节数组至数值转换

    LabVIEW程序,功能:将4字节的unsigned char输入组合成1个32-bit int值,若输入字节数不等于4则报错。

    stm32f103的4字节转float和float转4字节代码

    以上就是关于STM32F103上4字节转float和float转4字节的基本实现。在实际应用中,确保正确处理字节序和数据类型转换是至关重要的,以避免出现不期望的结果。在开发过程中,可以利用提供的代码作为基础,并根据具体...

    Java中byte转int的方法

    byte转化为int有两种情况:  1)要保持数值不变  应用场景:数值计算。等等。  方法:能够直接?用强制类型转换:int i = (int) aByte,  比如:若aByte=0xff(即数值为-1)。则转化为int后。i为0xffffffff。...

    Java将图片转换为byte数组例子总结

    本文将详细介绍如何使用Java将图片转换为`byte`数组以及如何将`byte`数组还原成图片。 #### 二、将图片转换为byte数组的方法 **方法名称**:`imageToBytes` **参数**: - `Image image`:表示输入的图片对象。 - ...

    Java实现字节流与图片的转化

    在Java编程中,字节流(Byte Stream)是处理数据的基本方式,特别是在处理二进制数据,如图片、音频或视频文件时。本教程将详细讲解如何使用Java实现字节流来转换和处理图片。 首先,我们需要理解字节流的概念。在...

    转换Image数据为byte数组

    当涉及到网络传输或存储时,将图像数据转换为字节数组(byte array)成为了一种实用且高效的手段。下面,我们将深入探讨如何在Java中实现图像数据与字节数组之间的相互转换,并分析这一过程中的关键步骤和技术细节。...

Global site tag (gtag.js) - Google Analytics