Introduction
In this lab, you will learn how to use the toCodePoint()
method of the Character
class in Java which will convert the specified surrogate pairs to its supplementary code point value.
In this lab, you will learn how to use the toCodePoint()
method of the Character
class in Java which will convert the specified surrogate pairs to its supplementary code point value.
Create a Java class CharToCodePoint
.
public class CharToCodePoint {
public static void main(String[] args) {
}
}
Declare variables, highOne
, lowOne
, highTwo
and lowTwo
, with char values to convert to codepoint.
public class CharToCodePoint {
public static void main(String[] args) {
char highOne = '\udd6f';
char lowOne = '\udc7e';
char highTwo = 'B';
char lowTwo = 'c';
}
}
Convert surrogate pair variables declared in step 2 to codepoints using the toCodePoint()
method.
public class CharToCodePoint {
public static void main(String[] args) {
char highOne = '\udd6f';
char lowOne = '\udc7e';
char highTwo = 'B';
char lowTwo = 'c';
int codePointOne = Character.toCodePoint(highOne, lowOne);
int codePointTwo = Character.toCodePoint(highTwo, lowTwo);
}
}
Print the variables, codePointOne
and codePointTwo
, in the console.
public class CharToCodePoint {
public static void main(String[] args) {
char highOne = '\udd6f';
char lowOne = '\udc7e';
char highTwo = 'B';
char lowTwo = 'c';
int codePointOne = Character.toCodePoint(highOne, lowOne);
int codePointTwo = Character.toCodePoint(highTwo, lowTwo);
System.out.println("Surrogate Pair converted to codePointOne : " + codePointOne);
System.out.println("Surrogate Pair converted to codePointTwo : " + codePointTwo);
}
}
Compile and run the code in the terminal.
javac CharToCodePoint.java
java CharToCodePoint
Change the input values in step 2 to check the conversion of codepoint for different surrogate pairs.
public class CharToCodePoint {
public static void main(String[] args) {
char highOne = '\ud800';
char lowOne = '\udc00';
char highTwo = 'A';
char lowTwo = 'b';
int codePointOne = Character.toCodePoint(highOne, lowOne);
int codePointTwo = Character.toCodePoint(highTwo, lowTwo);
System.out.println("Surrogate Pair converted to codePointOne : " + codePointOne);
System.out.println("Surrogate Pair converted to codePointTwo : " + codePointTwo);
}
}
Compile and run the modified code in the terminal.
javac CharToCodePoint.java
java CharToCodePoint
In this lab, you have learned how to use the toCodePoint()
method of the Character
class in Java to change the specified surrogate pairs to its supplementary code point value.