将无符号字节转换为有符号字节 [英] Convert unsigned byte to signed byte

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

问题描述

在Java中是否有一种简单而优雅的方法将无符号字节值转换为有符号字节值?例如,如果我仅有的是int值240(二进制(24位+ 11110000)= 32bits),如何获取该int的带符号值?

解决方案

char 外,Java没有未签名的值。考虑以下代码段:

  byte val =(byte)255; 
System.out.println(String.valueOf(val));

结果将为-1,因为最低的8位已复制到byte变量。 / p>

Is there an easy and elegant way to convert an unsigned byte value to a signed byte value in java? For example, if all I have is the int value 240 (in binary (24 bits + 11110000) = 32bits), how can I get the signed value for this int?

解决方案

Java does not have unsigned values, except for char. Consider this snippet:

byte val = (byte)255;
System.out.println(String.valueOf(val));

The result will be -1, because the lowest 8 bits got copied over to the byte variable.

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

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