How to subtract or add two hexadecimal value in java

99封情书 提交于 2020-02-22 07:44:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!