简体   繁体   English

为什么我可以将8位十六进制解析为Long,并将其转换为Integer,但不能直接解析为Integer?

[英]Why can I parse an 8 digit hex into a Long, and convert it to an Integer, but not parse as an Integer directly?

I am trying to use ColorDrawable(int color) . 我正在尝试使用ColorDrawable(int color) This class only has an int constructor. 该类只有一个int构造函数。 Normally, you can do something like this: 通常,你可以这样做:

ColorDrawable(0xFF8E8F8A)

But since I am getting my color as a String (6 hex digits, no alpha), I have to do this: 但由于我的颜色是字符串(6个十六进制数字,没有alpha),我必须这样做:

Long color = Long.parseLong("FF"+hexColorString, 16); // hexColorString like "8E8F8A"
ColorDrawable drawable = new ColorDrawable(color.intValue());

Why doesn't Integer.parseInt("FF"+hexColorString, 16) just return me a negative (effectively unsigned) int, instead of throwing a NumberFormatException ? 为什么Integer.parseInt("FF"+hexColorString, 16)只返回一个负数(有效无符号)int,而不是抛出NumberFormatException

EDIT: A more succinct version of my question: 编辑:我的问题更简洁的版本:

Why don't Long.parseLong("FF"+hexColorString, 16).intValue() and Integer.parseInt("FF"+hexColorString, 16) return the same value? 为什么Long.parseLong("FF"+hexColorString, 16).intValue()Integer.parseInt("FF"+hexColorString, 16)返回相同的值? The former works, but the latter gives me an Exception. 前者有效,但后者给了我一个例外。

EDIT: I wasn't getting the correct color anyway, so I switched to the following method: 编辑:无论如何我没有得到正确的颜色,所以我切换到以下方法:

ColorDrawable drawable = new ColorDrawable(Color.parseColor("#FF"+hexColorString));

The value of 0xFF8E8F8A is > Integer.MAX_VALUE . 0xFF8E8F8A的值> Integer.MAX_VALUE

Since no overflow or underflow throws Exception s by design, it will interpret your value as Integer.MIN_VALUE instead, because Integer.MAX_VALUE + 1 shifts to Integer.MIN_VALUE . 由于没有溢出或下溢按设计抛出Exception ,因此它会将您的值解释为Integer.MIN_VALUE ,因为Integer.MAX_VALUE + 1会转换为Integer.MIN_VALUE

So, Long.intValue will convert the value to int , which, with a given value of Integer.MAX_VALUE + x where x > 0 , will shift from Integer.MIN_VALUE , ie Integer.MIN_VALUE + x . 因此, Long.intValue会将值转换int ,其中给定值为Integer.MAX_VALUE + x ,其中x > 0 ,将从Integer.MIN_VALUE ,即Integer.MIN_VALUE + x

However, from Integer javadoc: 但是,来自Integer javadoc:

An exception of type NumberFormatException is thrown if any of the following situations occurs: 如果发生以下任何一种情况,则抛出NumberFormatException类型的异常:

The first argument is null or is a string of length zero. 第一个参数为null或者是长度为零的字符串。 [...] The value represented by the string is not a value of type int. [...]字符串表示的值不是int类型的值。

A value of 0xFF8E8F8A is not of type int , hence the NumberFormatException . 0xFF8E8F8A不是int类型,因此是NumberFormatException

As a side-note, I'm pretty sure ColorDrawable constructor takes an int because it takes an id instead of a numerical representation of your color, but to be honest the documentation isn't quite clear on that. 作为旁注,我很确定ColorDrawable构造函数接受一个int因为它需要一个id而不是颜色的数字表示,但说实话,文档并不十分清楚。

See R.color documentation here . 在此处查看R.color文档。

Final note - credit goes to OP on this one. 最后的注释 - 在这一点上归功于OP

You can use new ColorDrawable(Color.parseColor(yourHexString)) for a more convenient approach. 您可以使用new ColorDrawable(Color.parseColor(yourHexString))来获得更方便的方法。

Because 0xFF8E8F8A is outside of integer range. 因为0xFF8E8F8A超出整数范围。 Ie 0xFF8E8F8A == 4287532938 , and it is bigger than Integer.MAX_VALUE . 0xFF8E8F8A == 4287532938 ,它大于Integer.MAX_VALUE

Assumption that 0xFF8E8F8A equals to -7434358 (the value you get when parsing via Long) is not correct, because you can parse negative hex values: 假设0xFF8E8F8A等于-7434358 (通过Long解析时得到的值)不正确,因为您可以解析负十六进制值:

Integer.parseInt("-717076", 16);

So -0x717076 equals to -7434358 and unsigned representation of it is 0xFF8E8F8A . 所以-0x717076等于-7434358 ,无符号表示为0xFF8E8F8A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM