How to get hex value from decimal integer in Java?

I do not know how to generate the hexadecimal character "0x83" from an integer value in Java.

I need the value "0x83" to represent a letter in Cyrillic (this letter: ѓ ) in order to send it (letter) to my printer. When converting 131 (0x83 in decimal) to hex with my converter (below) I get three numbers: 0x31, 0x33 and 0x31.

public String toHex(String arg) { return String.format("%x", new BigInteger(arg.getBytes())); } 

I need to get 0x83 from this conversion.

+12
java int hex
Mar 10 '11 at 10:33
source share
6 answers

If you are trying to convert the integer 131 to the sixth line, you can try

 Integer.toHexString( 131 ) 

It will return “83” as a string.

+39
Mar 10 '11 at 10:41
source share

Here is an example:

 String str = Integer.toHexString(131); System.out.println(str); 
+12
Mar 10 '11 at 10:40
source share
 String cyrillic = Character.toString((char)0x83) 
+2
Apr 13 '11 at 12:51 on
source share

Have you tried checking out the Java Integer API . Here are some examples :

0
Mar 10 '11 at 10:40
source share

I do not see a problem with the conversion:

 System.out.println(Integer.toHexString(131)); 

returns 83.

0
Mar 10 '11 at 10:40
source share

Two possibilities: your printer requires 0x83 as a byte or as a string / char

Send as byte:

 int Cyrillic_int = 131; byte Cyrillic = (byte) Cyrillic_int; 

Or send the string representation 0x83:

 int Cyrillic_int = 131; String Cyrillic = Integer.toHexString(131); 
0
Sep 23 '13 at 16:18
source share



All Articles