1

I am reading a number from a file, and I want to convert it to an ASCII text string. I've seen questions relating to this but they all were dealing with single decimal-equivalent ASCII values, like 12 to ASCII, 87 to ASCII, 112 to ASCII, etc. I need to convert a large amount of decimals with no spaces into ASCII text. Can anyone show me how this is done? How would the system ascertain whether the first number to translate is 1, 12, or 123? For example:

int intval = 668976111;
//CONVERSION CODE
System.out.println(newval);

prints "bylo"

If the int 123, how would it know if I was saying 1,2,3 or 12,3 or 123 or 1,23 etc? How can I convert decimal numbers like this to Unicode characters?

  • 2
    This is totally unclear. Please reword and give some examples. – Eugene Sh. Mar 09 '16 at 18:33
  • Are you asking how to convert the integer value `112368122` to text, without using [`String.valueOf(intValue)`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf%28int%29) or [`Integer.toString(intValue)`](https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString%28int%29)? If so, then look at the source code of `Integer.toString()`. – Andreas Mar 09 '16 at 18:35
  • I dont mean like 1 to "1", I mean like 98 to "B" – CrafterOfWorlds Mar 09 '16 at 19:02
  • 3
    It won't work, as in decimal representation it is ambiguous, as you correctly point it out. – Eugene Sh. Mar 09 '16 at 19:11
  • Is there any way to do it? Maybe with another format? – CrafterOfWorlds Mar 09 '16 at 19:11
  • Use hexadecimal representation with fixed 2-digit width... – Eugene Sh. Mar 09 '16 at 19:12
  • I only have 0 - 9, no A - F – CrafterOfWorlds Mar 09 '16 at 19:14
  • What are you trying to achieve, at a higher level? This looks like an XY problem to me: you're asking about a solution you have in mind for a problem we don't know about, and the best solution to the actual problem is probably not the one you have in mind. So tell us the actual problem. – JB Nizet Mar 09 '16 at 19:15
  • My original intent was to turn Pi into letters... But NVM, answered. – CrafterOfWorlds Mar 09 '16 at 19:17
  • If that's the only constraint, turn 0 to 'a', 1 to 'b', etc. and the decimal point to 'k'. – JB Nizet Mar 09 '16 at 19:20
  • actually it should print `BYLo`, not `bylo` – njzk2 Mar 09 '16 at 19:22
  • You should store your encoded string as an array of `byte`s (which can be an arbitrary length and is unambiguous), then [use the `String` constructor to decode the byte array into a string](http://stackoverflow.com/questions/88838/how-to-convert-strings-to-and-from-utf8-byte-arrays-in-java) – Colonel Thirty Two Mar 09 '16 at 22:58

2 Answers2

0

This is a kinda hard problem, as ascii codes can be single, two or three digit long.

Now if your encoding only alphadecimal characters and characters above the decimal number 20 it is pretty easy.

The algorithm wouild be as follows. Iterate through the array(a digit is an element of the array), if the first number is 1, take 3 numbers, as you cant have a char with code less than 20. If the first number is higher than 20, take only 2 numbers.

This way you will get the right decoding, assuming you dont have anything encoded with codes less than 20, which is a very possible assumption, as the first "useful" code is at number 32, which is space

0

Try this.

static void decode(String s, int index, byte[] decode, int size) {
    if (index >= s.length())
        System.out.println(new String(decode, 0, size));
    else
        for (int i = index + 1; i <= s.length(); ++i) {
            int d = Integer.parseInt(s.substring(index, i));
            if (Character.isISOControl(d)) continue;
            if (d > 255) break;
            decode[size] = (byte)d;
            decode(s, i, decode, size + 1);
        }
}

static void decode(String s) {
    decode(s, 0, new byte[s.length()], 0);
}

and

    decode("668976111");    // -> BYLo