Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java int to hex with 0x

Tags:

java

hex

zero

I'm trying to convert int to hex with format 0x12 0x2B and so on. Is there anything similar like python: https://docs.python.org/2/library/functions.html#hex to accomplish this or i will need to work around this with many unnecessary steps?

I need to get something like this below:

int []hexInt={0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01}; 
like image 901
Anak1n Avatar asked Aug 30 '26 09:08

Anak1n


2 Answers

You could declare a String which will be equal to "0x" + Integer.toHexString(int)

like image 121
Aradmey Avatar answered Sep 03 '26 06:09

Aradmey


public static String intToHexStr(int i){
    return "0x"+String.format("%2s", Integer.toHexString(i)).replace(' ', '0');
}
like image 32
JaskeyLam Avatar answered Sep 03 '26 05:09

JaskeyLam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!