Java将int隐式转换为字节 [英] Java implicit conversion of int to byte

查看:131
本文介绍了Java将int隐式转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将开始处理需要读取字节和创建字符串的东西。正在读取的字节表示UTF-16字符串。所以只是为了测试的东西,我想将一个简单的UTF-16编码的字节数组转换为字符串。数组中的前2个字节必须表示字节序,因此必须为0xff 0xfe或0xfe 0xff。所以我试着创建我的字节数组如下:

  byte [] bytes = new byte [] {0xff,0xfe,0x52, 0x00,0x6F,0x00}; 

但我有一个错误,因为0xFF和0xFE太大,不适合一个字节登录Java)。更准确地说,错误是int不能转换为一个字节。我知道我可以显式转换从int到字节与转换和实现所需的结果,但这不是我的问题是关于。



只是试试out我创建了一个字符串并调用getBytes(UTF-16)然后打印数组中的每个字节。输出有些混乱,因为前两个字节是0xFFFFFFFE 0xFFFFFFFF,后跟0x00 0x52 0x00 0x6F。 (obvisouly这里的字节顺序与我以前尝试创建的不同,但这不重要)。



使用这个输出我决定尝试创建我的字节数组同样的方式:

  byte [] bytes = new byte [] {0xffffffff,0xfffffffe,0x52,0x00,0x6F,0x00} 

奇怪的是它工作得很好。所以我的问题是,为什么Java允许一个整数值0xFFFFFF80或更大的自动转换为一个字节没有显式转换,但任何等于或大于0x80要求显式转换?


< Java中的 int 是一个有符号的值。当您分配 0xffffffff (这是 2 ^ 32 -1 )时,将转换为值为 -1 - int 实际上不能表示 0xffffffff 作为正数。



因此,对于小于0x80和大于0xFFFFFF80的值,所产生的 int 在-128和127之间,它可以明确地表示为 byte 。该范围之外的任何内容都不能,而且需要强制使用显式转换,在该过程中丢失数据。


I am about to start working on something the requires reading bytes and creating strings. The bytes being read represent UTF-16 strings. So just to test things out I wanted to convert a simple byte array in UTF-16 encoding to a string. The first 2 bytes in the array must represent the endianness and so must be either 0xff 0xfe or 0xfe 0xff. So I tried creating my byte array as follows:

byte[] bytes = new byte[] {0xff, 0xfe, 0x52, 0x00, 0x6F, 0x00};

But I got an error because 0xFF and 0xFE are too big to fit into a byte (because bytes are signed in Java). More precisely the error was that the int couldn't be converted to a byte. I know that I could just explicitly convert from int to byte with a cast and achieve the desired result, but that is not what my question is about.

Just to try something out I created a String and called getBytes("UTF-16") then printed each of the bytes in the array. The output was slightly confusing because the first two bytes were 0xFFFFFFFE 0xFFFFFFFF, followed by 0x00 0x52 0x00 0x6F. (Obvisouly the endianness here is different from what I was trying to create above but that is not important).

Using this output I decided to try and create my byte array the same way:

byte[] bytes = new byte[] {0xffffffff, 0xfffffffe, 0x52, 0x00, 0x6F, 0x00};

And strangely enough it worked fine. So my question is, why does Java allow an integer value of 0xFFFFFF80 or greater to be automatically converted to a byte without an explicit cast, but anything equal to or greater than 0x80 requires an explicit cast?

解决方案

The key thing to remember here is that int in Java is a signed value. When you assign 0xffffffff (which is 2^32 -1), this is translated into a signed int of value -1 - an int cannot actually represent something as large as 0xffffffff as a positive number.

So for values less than 0x80 and greater than 0xFFFFFF80, the resulting int value is between -128 and 127, which can unambiguously be represented as a byte. Anything outside that range cannot be, and needs forcing with an explicit cast, losing data in the process.

这篇关于Java将int隐式转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆