How to get a signed 32 bit integer from hex bytes without being 2's complement applied to it in java

和自甴很熟 提交于 2020-06-09 05:50:06

问题


I have 4bytes of hex values directly without two's complement being applied to signed inetger. How do I get an int value from hex bytes using java.


回答1:


Doesn't this work?

int value = ((0xff & b4) < 24) | ((0xff & b3) < 16) | ((0xff & b2) < 8) | (0xff & b1);



回答2:


Use a Long. You can't use an int as it's always signed and 4 bytes just won't fit.

    Long l = Long.parseLong("FFFFFFFF", 16);


来源:https://stackoverflow.com/questions/7662819/how-to-get-a-signed-32-bit-integer-from-hex-bytes-without-being-2s-complement-a

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