Reputation: 28379
I'm really confused by the fact that TextView.setTextColor(int)
is perfectly ok with
0xFFFFFFFF as an input value
but is not ok with the decimal equivalent of
4294967295
Now, I totally understand why that second value is totally out of range for an int. My question is why the hex value is NOT out of range!
The reason I ask is that I have a situation where I'm reading hex values from a text file that I need to use at runtime to set the colors of various text fields in the app.
I'm running into a wall trying the various string to int methods in Java (Long.parseLong
, et al), but none of them are doing the trick.
What's the right way to convert a string hex value into something that setTextColor will be happy with and will interpret correctly?
Upvotes: 1
Views: 114
Reputation: 26185
It is a matter of different rules in the JLS for different forms of Integer Literals
"It is a compile-time error if a decimal literal of type int is larger than 2147483648 (231), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4)."
"It is a compile-time error if a hexadecimal, octal, or binary int literal does not fit in 32 bits."
A hex, octal, or binary literal can be the 2's complement representation of a negative number.
Upvotes: 1
Reputation: 29436
Maybe Color
class will help :
Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributions from red, green, or blue), and opaque-white would be 0xFFFFFFFF
Upvotes: 0