3

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}; 
Dharman
  • 30,962
  • 25
  • 85
  • 135
Anak1n
  • 149
  • 1
  • 2
  • 12
  • 2
    Do you mean you want a `String` representation of the number as hex? – Sotirios Delimanolis Feb 07 '15 at 22:26
  • possible duplicate of [Java Convert integer to hex integer](http://stackoverflow.com/questions/9321553/java-convert-integer-to-hex-integer) – HelloWorld Feb 07 '15 at 22:29
  • I need int as show int [] hexInt={0x00 and so on} – Anak1n Feb 07 '15 at 22:30
  • it is not duplicate. I can convert integer to hex but i can not convert it to hex like 0x1b. If i use 1B insted of 0x1B then i'm not getting what i need. – Anak1n Feb 07 '15 at 22:31
  • Shown where? I don't understand your goal. Are you declaring an `int[]` or you want to display those characters? – Sotirios Delimanolis Feb 07 '15 at 22:31
  • Your array is just an ordinary array of `int`s. The fact that you have typed them using hex literals is dealt with by the compiler and gone at run-time. – 5gon12eder Feb 07 '15 at 22:32
  • I know that. But when i send to smpp commands like int it isnt working but when send commands like 0x00 evrything works fine. So i know what i need. – Anak1n Feb 07 '15 at 22:33
  • And that command is Enquire Link – Anak1n Feb 07 '15 at 22:34
  • Your question is very unclear. It is not even clear to me whether you want a string or an integer. – 5gon12eder Feb 07 '15 at 22:34
  • How you write value of int doesn't really matter, it will still represent same number. If you use array with `{0x10, 16}` will contain same numbers. Please provide more details about what you want to achieve. Do you wan to print decimal int like `16` as hexadecimal `0x10`? – Pshemo Feb 07 '15 at 22:34
  • Ok i'm writing it to a socket. I'm using DataOutputStream when i type manualy like in above example: int []hexInt={0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01}; output.write(hexInt[i]); i'm getting response from smpp server enquireLink response if i use it with out 0x it is not recognized. So i dont want to type it every time i need to send data to the smpp server. – Anak1n Feb 07 '15 at 22:36
  • @ 5gon12ederyou are right i sent int and got response dunno why did i mess with hex. Thank you. – Anak1n Feb 07 '15 at 23:43

3 Answers3

6

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

HelloWorld
  • 1,128
  • 1
  • 7
  • 14
  • i did that but when i want to parse string to int it can not parse 0x1b to int it throws number format exception but it accept array int [] hexInt={0x1b} i'm confused. – Anak1n Feb 07 '15 at 22:35
  • Do what I said above and then store it that way `Integer.parseInt(num.substring(2), 16);`. This will remove the `0x` from the string before parsing. – HelloWorld Feb 07 '15 at 22:37
  • 1
    @Anak1n To parse hexString back you decimal int you need to remove `0x` part and provide radix like `Integer.parseInt("100",16)` will be parsed to `256`. – Pshemo Feb 07 '15 at 22:37
  • `Integer.decode` will actually properly handle the 0x prefix. – Louis Wasserman Feb 07 '15 at 23:39
1
public static String intToHexStr(int i){
    return "0x"+String.format("%2s", Integer.toHexString(i)).replace(' ', '0');
}
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
0

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

Integer.parseInt(String.format("%X", intValue),16)
Dharman
  • 30,962
  • 25
  • 85
  • 135
patel dhruval
  • 1,002
  • 10
  • 12