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

AntonH
  • 6,359
  • 2
  • 30
  • 40
kbz
  • 984
  • 2
  • 12
  • 30
  • 1
    What does this have to do with regex? – Ant P Apr 27 '14 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. – kbz Apr 27 '14 at 16:46
  • 1
    Are there always going to be three digits per ASCII character? So 'A' (65) is "065" in your input? – Jongware Apr 27 '14 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. – Ant P Apr 27 '14 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.) – Jongware Apr 27 '14 at 16:56
  • It's perfectly possible as I've managed it before but I have lost the regex that I used. It uses the lookforward and lookbehind functions I believe – kbz Apr 27 '14 at 16:59

1 Answers1

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.

Community
  • 1
  • 1
flotothemoon
  • 1,882
  • 2
  • 20
  • 37
  • 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. – kbz Apr 27 '14 at 16:58
  • @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. – flotothemoon Apr 27 '14 at 17:51
  • Yes, the reverse of what you've just said. So if "test" is "84101115116", how would I turn "84101115116" back to test? – kbz Apr 27 '14 at 20:01