There’s a byte[4] containing char codes for random hex numbers, i.e. b[0] == 49, b[1] == 68, b[2] == 70 …
I’d like to calculate something with these hex values, so I need to cast the char values to int. That sounds simple, but my solution looks terrible:
char c1 = ((char)b[0]);
String s1 = String.valueOf(c1);
int p1 = Integer.parseInt(s1);
char c2 = ((char)b[1]);
String s2 = String.valueOf(c2);
int p2 = Integer.parseInt(s2);
...
Isn’t there a shorter method? There’s so many generic key words in my question that I can’t find a solution through google either.
Updated post:
Whoops, I forgot to mention the byte[] contains hex values. So
int x = b[0] - '0';
won’t suffice.
How about:
Oh, wait. You want to interpret the characters as digits and use the values. If you’re sure that the byte values are ASCII digit codes, you can do this:
If you want a formally correct way to do it:
This will correctly handle hex characters (0, 1, …, F) and even things like
'\u216C'(ROMAN NUMERAL FIFTY).