简体   繁体   中英

How to subtract or add two hexadecimal value in java

Is there a way of calculating two hexadecimal value without converting it to int? for example:

String sHex = "f7c0";
String bHex = "040000000";

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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