0

Given a String of numerical ascii values, how can I convert these back to their original characters to generate a String. I understand how this works, but I don't understand the regex that I would need to use to complete this task.

Example:

"108108108108"

108 represents the character "l", so the output would be: llll

6
  • 1
    What does this have to do with regex? Commented Apr 27, 2014 at 16:44
  • I understand how to convert a single char to a String, but I need a String of these numerical values to be converted back. I don't know how to split them into individual values. Commented Apr 27, 2014 at 16:46
  • 1
    Are there always going to be three digits per ASCII character? So 'A' (65) is "065" in your input? Commented Apr 27, 2014 at 16:48
  • 2
    If they're just in a string like that, you can't. It's impossible to distinguish between "108/108" and, say, "108/10/8" unless you know in advance that each character is denoted by 3 characters, in which case you need to split the string up into sets of 3 characters, which still has nothing to do with regular expressions. Commented Apr 27, 2014 at 16:49
  • @AntP: it can be done, but only with a constraint such as "each of the ASCII values must be 32 <= x <= 127". (Also, a regex can help.) Commented Apr 27, 2014 at 16:56

1 Answer 1

1

Character.toString((char) i);

i is the ASCII value of the character.

Character.toString((char) 108); would be 'i'.

You might want to take a look at this: How to convert ASCII code (0-255) to a String of the associated character?

But if you dont know if 1087 means 108 and 7 or 10 and 87, like if you cant be sure that the values are saved like 001 and 002 for 1 and 2, you can't.

The function would have to be able to differbetween those cases, but however it doesn't know if you mean 10 and 87 or 108 and 7 or 1 and 87.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I believe you misunderstood the question. I need to convert a full String of ascii values back to their characters. "1095611108782", all of those numbers would output a word.
@Kieran I am not sure what you mean. Like, "test" would be "84101115116" or "test" would be "084101115116". If it's formatted like "001", "002", ... and each block has the same length, you could do it, but otherwise you would have to set constraints on your values.
Yes, the reverse of what you've just said. So if "test" is "84101115116", how would I turn "84101115116" back to test?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.