Samuel
Samuel

Reputation: 60

How to know ASCII / UTF code of emojis?

I need to obtain the codes of the emojis entered on an EditText in an Android App. I've tried this, but it always returns me ASCII values 55356 and 55357 for all the emojis.

    end   = (TextView) findViewById(R.id.txtFinal);
    botom = (Button) findViewById(R.id.btn);
    intro = (EditText)findViewById(R.id.txtIntro);

    botom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String t = intro.getText().toString();
            if (t != null && t != ""){
                char c = t.charAt(0);
                end.setText((int)c+"");
            }
        }
    });

Can I convert them to UTF? If so, how? Or there is another enconding specific for emojis?

Upvotes: 0

Views: 2411

Answers (2)

Tom Blodget
Tom Blodget

Reputation: 20782

Forget about ASCII. If you only think you are using ASCII, you're almost certainly not.

charAt does not return ASCII values. It returns a UTF-16 code unit, one or two of which encode a Unicode codepoint.

It's unclear what you want to do with the emoji. It seems that you are splitting it out of the whole string and assuming it is the first text element in the string. Maybe that's just test code.

You can take UTF-16 code units until you have a whole character. That's one or two code units. The Character class has a method for that: codePointAt(CharSequence seq, int index). There are other methods that you could use to depending on what you are trying to accomplish. The class description is good reading.

Upvotes: 1

McAngus
McAngus

Reputation: 1856

You could change the encoding by using t.getBytes("UTF8"). Here is some more info on the getBytes method.

Upvotes: 0

Related Questions