A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
- For 1-byte character, the first bit is a 0, followed by its unicode code.
- For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.
Given an array of integers representing the data, lets calculate whether it is a valid utf-8 encoding.
Example:
data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100.
Return false.
The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character.
The next byte is a continuation byte which starts with 10 and that's correct.
But the second continuation byte does not start with 10, so it is invalid.
Source Code:
public static void main(String[] args) {
// should be true
System.out.println(validUtf8(new int[]{228, 189, 160, 229, 165, 189, 13, 10}));
//should be false
System.out.println(validUtf8(new int[]{240, 162, 138, 147, 145}));
//should be true
System.out.println(validUtf8(new int[]{197, 130, 1}));
//should be false
System.out.println(validUtf8(new int[]{235, 140, 4}));
//should be false
System.out.println(validUtf8(new int[]{255}));
//should be false
System.out.println(validUtf8(new int[]{250, 145, 145, 145, 145}));
}
public static boolean validUtf8(int[] data) {
int nOctetLeft = 0;
for (int i = 0; i < data.length; ++i) {
int octet = data[i];
// n-byte character
if ((octet & (1 << 7)) != 0) {
int n = getCount(octet);
if (n > 4) {
return false;
}
if (n > 1) {
if (nOctetLeft > 0) {
return false;
}
nOctetLeft += n;
--nOctetLeft;
} else if (n == 1) {
--nOctetLeft;
}
} else {
// one-byte character
if (nOctetLeft != 0) {
return false;
}
}
}
// unsatisfied header
if (nOctetLeft != 0) {
return false;
}
return true;
}
private static int getCount(int octet) {
int count = 0;
int i = 7;
while (i >= 0) {
if ((octet & (1 << i)) == 0) {
break;
} else {
++count;
}
--i;
}
return count;
}
Comments