13

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.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Ballon
  • 6,882
  • 18
  • 49
  • 63
  • 3
    It's entirely unclear exactly what you're doing. It sounds like you're converting the *string* "131" to bytes, which isn't the same thing at all. Please show sample code. – Jon Skeet Mar 10 '11 at 10:35
  • I send parameter which is integer and after adding 128 to get value greater then 128 => 131 and convert but its wrong. Because i can't insert Cyrillic alphabet in eclipse and convert. – Ballon Mar 10 '11 at 10:38
  • public String toHex(String arg) { return String.format("%x", new BigInteger(arg.getBytes())); } – Ballon Mar 10 '11 at 10:44
  • Then what most of the answers here say i.e., `Integer.toHexString` should solve your problem! – adarshr Mar 10 '11 at 10:47
  • @Gogoo: Don't put the code in comments - edit your question to make it clearer. – Jon Skeet Mar 10 '11 at 10:47
  • 1
    You claim that you "send parameter which is integer" but the code you've provided in the comment takes a *string* parameter, not an integer. Please be precise, otherwise it'll be hard to help you. – Jon Skeet Mar 10 '11 at 10:48
  • No, because I use this method for all other function in my program. – Ballon Mar 10 '11 at 11:03
  • Ok to be precise which parameter I need to send to my method in comment to get 0x83. Or how to send to get 0x83. Because that parameter I can't insert in my program here is link http://www.ascii.ca/cp1251.htm – Ballon Mar 10 '11 at 11:05
  • Ok so you want to send one parameter to printer that returns 0x83 in return. Reply hurry. – Ankit Mar 10 '11 at 11:20
  • I wnat to send one parameter to my method( public String toHex(String arg) { return String.format("%x", new BigInteger(arg.getBytes())); } ) which will convert the input parameter and return 0x83. – Ballon Mar 10 '11 at 11:23
  • Or I need to change encoding table? Or method but other character to stay the same like before. – Ballon Mar 10 '11 at 11:26
  • Is this because I need more than 1 byte to present (ѓ). – Ballon Mar 10 '11 at 11:29
  • I try like this: toHex(""+(char)131); but I get 0x3f. – Ballon Mar 10 '11 at 11:32
  • Do you really want to send the hexadecimal string `"0x83"` to your printer, or do you want to send the byte with this value `0x83 = 131` to your printer? – Paŭlo Ebermann Mar 10 '11 at 12:53

6 Answers6

43

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

Integer.toHexString( 131 )

It will return "83" as String.

Nishan
  • 2,821
  • 4
  • 27
  • 36
13

Here's one example:

String str = Integer.toHexString(131);
System.out.println(str);
Alex
  • 8,093
  • 6
  • 49
  • 79
Ankit
  • 2,753
  • 1
  • 19
  • 26
  • public String toHex(String arg) { return String.format("%x", new BigInteger(arg.getBytes())); } I use this converter... – Ballon Mar 10 '11 at 10:42
  • @Gogoo :- Just debug the code and watch the line arg.getBytes() it generates ascii values for each digit hence the output comes 31,33,31. – Ankit Mar 10 '11 at 10:49
  • I need to get 0x83. Or how to send and what value to get 0x83. Because (ѓ) I can't insert in my program here is link http://ascii.ca/cp1251.htm – Ballon Mar 10 '11 at 11:10
2
String cyrillic = Character.toString((char)0x83)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
JJ3
  • 21
  • 1
0

Two possibilities, either your printer needs 0x83 as a byte or as string/char

Send as a byte:

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

Or send a string representation of 0x83:

int Cyrillic_int = 131;
String Cyrillic = Integer.toHexString(131);
Hammad
  • 23
  • 7
0

Have you tried checking out the Java Integer API. Here are a couple of examples:

posdef
  • 6,498
  • 11
  • 46
  • 94
0

I don't see a problem, when converting:

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

returns 83.

ilalex
  • 3,018
  • 2
  • 24
  • 37
  • public String toHex(String arg) { return String.format("%x", new BigInteger(arg.getBytes())); } I use this converter... – Ballon Mar 10 '11 at 10:43
  • 1
    @Gogoo Can you try with `String.format("%x", Integer.parseInt(s))` – Nishan Mar 10 '11 at 11:48