0

I am making an ASCII Encoder-Decoder. I am encoding the characters into UTF-8. To encode I am using this code:

private String asciiReturn(String inpString){
int codePoint = 0;
StringBuilder str = new StringBuilder();
for (int i = 0; i < inpString.length(); i++){
codePoint = Character.codePointAt(inpString, i);
i += Character.charCount(codePoint) - 1;
str.append(codePoint);
str.append(" ");
}
return str.toString();
}

So by this, I can encode all those emoji characters too.

Like '‍♂️' for this emoji I am getting "129335 127995 8205 9794 65039". So this is basically the UTF-8 decimal value of the emoji and that's exactly what I want. But my problem is the decoding.

What I want is: (Example)

Input String: "72 117 104 33 129335 127995 8205 9794 65039"
Output String: "Huh!‍♂️"

Cause:
72 -> 'H'

117 -> 'u'

104 -> 'h'

33 -> '!'

129335 127995 8205 9794 65039 -> '‍♂️'

Thanks in advance

Jeet Nath
  • 27
  • 1
  • 7
  • You need to split by space, parse each integer and then use StringBuilder::appendCodePoint method. Give it a try and if it not working someone will help. – Daniel F Aug 09 '20 at 05:38
  • This has nothing to do with 'extended ASCII'. Extended ASCII is the set of single byte character sets that have ASCII as byte 0 - 127. None of the extended ASCII character sets support emoji. In any case, you need to put in some effort: what have you tried, and where are you stuck. – Mark Rotteveel Aug 09 '20 at 05:48

1 Answers1

1

Try this.

private String decode(String inpString) {
    return Arrays.stream(inpString.split("\\s+"))
        .map(s -> Character.toString(Integer.parseInt(s)))
        .collect(Collectors.joining());
}

and

String input = "72 117 104 33 129335 127995 8205 9794 65039";
System.out.println(decode(input));

output

Huh!‍♂️

You can also write your encoding method like this:

static String asciiReturn(String s) {
    return s.codePoints()
        .mapToObj(Integer::toString)
        .collect(Collectors.joining(" "));
}

and

String s = "Huh!‍♂️";
System.out.println(asciiReturn(s));

output

72 117 104 33 129335 127995 8205 9794 65039
  • WOW!! It worked, Thank You, very much...just one more request Please, sir... "Please explain what is actually happening in the split function?" – Jeet Nath Aug 09 '20 at 06:48
  • `.split("\\s+")` splits a string with spaces and stores it in an array of strings. `\\s+` is a regular expression that represents one or more consecutive spaces. –  Aug 09 '20 at 07:14