Anak1n
Anak1n

Reputation: 149

Java int to hex with 0x

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

Upvotes: 3

Views: 13148

Answers (3)

patel dhruval
patel dhruval

Reputation: 1052

Maybe this is very late but here is the answer to your question:

Integer.parseInt(String.format("%X", intValue),16)

Upvotes: 1

JaskeyLam
JaskeyLam

Reputation: 15755

public static String intToHexStr(int i){
    return "0x"+String.format("%2s", Integer.toHexString(i)).replace(' ', '0');
}

Upvotes: 1

HelloWorld
HelloWorld

Reputation: 1103

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

Upvotes: 6

Related Questions