1

I have an API that sends data back & forth between client and server. On the client side there is a parser that takes this data and puts it into a properties file.

I have converted the string data on the server side to a hex integer to avoid getting in trouble with my parser because I split my data on characters like ";" and "_". So if my data contains any of those characters it will cause problems with my parser.

server send: SC;4b6579_56616c7565;4b65790a4e65776c696e65_56616c75650a4e65776c696e65;4b65795f556e64657273636f7265_56616c75655f556e64657273636f7265
client received: SC;4b6579_56616c7565;4b65790a4e65776c696e65_56616c75650a4e65776c696e65;4b65795f556e64657273636f7265_56616c75655f556e64657273636f7265

So some sample data would be

String key = "4b6579";
String value = "56616c7565";

int hexKey = Integer.parseInt(key, 16);
int hexValue = Integer.parseInt(value, 16);

How do I turn my hexKey and hexValue into strings?

The result should be as below:

  • 4b6579 converts to the word Key
  • 56616c7565 converts to the word Value

EDIT:

The answer was found here: Convert a String of Hex into ASCII in Java

Community
  • 1
  • 1
JREN
  • 3,572
  • 3
  • 27
  • 45
  • Usually it's better to use something commonly used instead of custom implementation. E.g. you can use JSON serialized to BASE64 for eliminate (de)serialization and different encodings problem. Also could you provide you server code which converts server data to content you are sending. – yatul Jun 20 '13 at 13:21
  • So you're trying to convert the specific value "4b6579" to the specific string "key"? In that case, you should just map the value to a string. You could use a simple switch statement. – Matt Becker Jun 20 '13 at 13:22
  • @GilbertLeBlanc this is a duplicate of your link, thank you. That should fix my problem. – JREN Jun 20 '13 at 13:26
  • The answer can be found in the solution of this question: http://stackoverflow.com/questions/4785654/convert-a-string-of-hex-into-ascii-in-java – JREN Jun 20 '13 at 13:33

3 Answers3

2

Here's something that could help:

String hex = "2A"; //The answer is 42  
int intValue = Integer.parseInt(hex, 16);

//this is the line you want
String hex = Integer.toHexString(42);//pass in int here

Also, you could easily use hex numbers in Java instead of Strings.

int[] x = {0xA4, 0x21};

EDIT: If the original question meant to ask how to convert hex into ASCII, then I'd recommend checking out the following:

Convert a String of Hex into ASCII in Java

Sources:

http://www.codebeach.com/2008/02/convert-hex-string-to-integer-and-back.html

Community
  • 1
  • 1
1

If you want to turn the hexKey and hexValue to String, just do this.

String.valueOf(hexKey);
String.valueOf(hexValue);

Instead of Integer.parseInt(key, 16), use Long.parseLong(key, 16);

darijan
  • 9,725
  • 25
  • 38
  • for hexKey this gives me 4941177 which is not the correct value and for hexValue it throws a NumberFormatException. So this doesn't work. – JREN Jun 20 '13 at 13:11
  • Well, your `Integer.parseInt` throws an exception. – darijan Jun 20 '13 at 13:12
  • The fact remains that String.valueOf(hexKey) does not convert my hexKey to the word Key but to 4941177 – JREN Jun 20 '13 at 13:15
  • @JorisRenting Your question is not clear. Could you tell us what Strings you want to get from `hexKey` and `hexValue`? – Pshemo Jun 20 '13 at 13:18
  • @Pshemo I wrote it in the question... 4b6579 converts to the word key 56616c7565 converts to the word value – JREN Jun 20 '13 at 13:21
  • And how should java know that this particular numbers has to become word "key". Then you asked your question wrong. The question is" "how to map certain numbers to certain words?" The answer is: "use a map". – darijan Jun 20 '13 at 13:22
  • 4b6579 is hex code for the word "key". Try running it through a hex to string converter: http://www.string-functions.com/hex-string.aspx – JREN Jun 20 '13 at 13:24
  • @JorisRenting Are you sure? Even if what you are asking is converting hex to ASCII (which is not obvious from your question) then it would be `Key` and `Value`. – Pshemo Jun 20 '13 at 13:26
  • 1
    @JorisRenting then your answer is [here](http://stackoverflow.com/questions/4785654/convert-a-string-of-hex-into-ascii-in-java) – Pshemo Jun 20 '13 at 13:28
0
String hex = Integer.toHexString(hexKey);

http://www.codebeach.com/2008/02/convert-hex-string-to-integer-and-back.html

Matt Becker
  • 2,338
  • 1
  • 28
  • 36
  • this returns KEY: 4b6579 for me, which is just the contents of hexKey converted from int to String. So this doesn't work. – JREN Jun 20 '13 at 13:13
  • I was just answering the question: "How to convert a hex integer to string" There is no hex integer, there's just an integer represented in hex format. – Matt Becker Jun 20 '13 at 13:14
  • But my question is how do I convert the hex value to a string? I think the question was elaborated quite clearly. – JREN Jun 20 '13 at 13:17
  • 4b6579 converts to the string "key", simply mapping it with a switch statement doesn't do me any good because I need to be able to convert any hex code to a string – JREN Jun 20 '13 at 13:23
  • And I answered your elaborated question. It was you who failed to state what kind of string. You simply said string. If you had asked how to convert hex into ASCII, then darijan, Mohammad and I wouldn't have been confused. However, I'm not going to mark the question down, that wouldn't be nice. – Matt Becker Jun 20 '13 at 13:39
  • If I knew that I had to look for hex into ASCII I would have found the answer to my question without having to create this one. Pshemo and Gillbert Le Blanc were able to figure out and help me with my problem. I didn't fail at anything because my question was answered and I can continue with my work now. But it wasn't you who was able to help me ;) – JREN Jun 20 '13 at 13:44