问题
Is there a way of calculating two hexadecimal value without converting it to int? for example:
String sHex = "f7c0";
String bHex = "040000000";
回答1:
Hexadecimal values are integers - just represented in hex instead of decimal.
Can't you just do this?
int sHex = 0xf7c0;
int bHex = 0x040000000;
If not, then you actually meant:
String sHex = "f7c0";
String bHex = "040000000";
In which case, the fastest way to do this is still by converting them to integers, using something like Integer.parseInt(sHex, 16);
回答2:
Yes, you can do certain (to be exact: any) calculations with strings that hold representations of hexadecimal values.
An easy example would be checking for equality. For this, you convert both strings to lowercase, strip all leading zeroes and apply the String.equals method.
Other calculations are more difficult. But hey, when I was young, we did all calculation with paper and pen or with chalk on the board, hence in principle anything that can be computed even if all data have the form of character strings.
来源:https://stackoverflow.com/questions/8245156/how-to-subtract-or-add-two-hexadecimal-value-in-java