Carlos Blanco
Carlos Blanco

Reputation: 8772

Create a char from a string that contains the utf code

I'm trying to create a char out of the utf code. I'm reading this code from a file which is a map of characters. All characters are specified by their UTF code.

0020 SPACE
0021 EXCLAMATION MARK
0022 QUOTATION MARK
.
.
.

After reading the code from the file, I end up with this code in a String. How can I convert this code(Stirng) to a char?

Upvotes: 2

Views: 271

Answers (3)

Mark Byers
Mark Byers

Reputation: 839044

The codes are stored in hexadecimal so I think you want this:

String code = "0021";
char c = (char)Integer.parseInt(code, 16);
System.out.println("Code: " + code + " Character: " + c);

I assume that none of your character codes exceed the maximum value that can be stored in a char, i.e. the characters in the Basic Multilingual Plane. Because your data format appears to be zero padded up to a maximum length of 4 hexadecimal digits, I assume that all the characters you need to consider are in fact in the BMP.

If this is not the case, you will need a different solution. See Character.toChars(int).

Upvotes: 4

Andreas Dolk
Andreas Dolk

Reputation: 114817

It looks like UTF-16. To create a String from these bytes, use:

new String(byte[]{0x00, 0x21}, "UTF-16")

This creates a String which holds the exclamation mark. The character is charAt(0).

EDIT

might not be the most performant approach but it works for other encodings as well...

EDIT

OK, there was a misunderstanding, the above code was not a solution but an example on how to faciliate the String constructor to create a String from a series of bytes in a special encoding. As it's an example, it looked static. Here's the runtime solution (knowing that especially the accepted solution fits much better - this one is just more general):

public char decodeUTF16(byte b1, byte b2) {
  return decode(new byte[]{b1, b2}).charAt(0);
}

public String decodeUTF16(byte[] bytes) {
  return decode(bytes, "UTF-16");
}

public String decode(byte[] bytes, String encoding) {
  return new String(bytes, encoding);
}

Upvotes: 0

zneak
zneak

Reputation: 138251

Parse it into an integer using Integer.parseInt(String, 16), then cast it to a char.

Upvotes: 1

Related Questions