Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a hex value from a decimal integer in Java?

Tags:

java

int

hex

I don't know how to generate a hex "0x83" character from an integer value in Java.

I need a "0x83" value to represent a letter in the Cyrillic alphabet (this letter: ѓ), in order to send it (the letter) to my printer. When converting 131 (0x83 in decimal) into 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.

like image 752
Ballon Avatar asked Mar 10 '11 10:03

Ballon


People also ask

How do you convert decimal to hexadecimal in Java?

To convert decimal to hexadecimal, use any of the two methods i.e. Integer. toHexString() − It returns a string representation of the integer argument as an unsigned integer in base 16. Integer.

Which method converts an int value into its equivalent hexadecimal?

An integer can be converted to a hexadecimal by using the string. ToString() extension method.

Can I store a hex in an int?

There is no special type of data type to store Hexadecimal values in C programming, Hexadecimal number is an integer value and you can store it in the integral type of data types (char, short or int).


1 Answers

If you are trying to convert integer 131 to a hex string, you can try

Integer.toHexString( 131 )

It will return "83" as String.

like image 178
Nishan Avatar answered Oct 16 '22 19:10

Nishan