当前位置:网站首页>Utf8 encoding

Utf8 encoding

2022-07-05 06:32:00 happytree001

utf8 Coding is a way of coding that becomes , Minimum 1 byte , The longest 4 byte .

 Please add a picture description
The following is python3.10.2 Middle to character utf8 Check function

/* Check whether the characters at s start a valid UTF-8 sequence. Return the number of characters forming the sequence if yes, 0 if not. */
static int valid_utf8(const unsigned char* s)
{
    
    int expected = 0;
    int length;
    if (*s < 0x80)
        /* single-byte code */
        return 1;
    if (*s < 0xc0)
        /* following byte */
        return 0;
    if (*s < 0xE0)
        expected = 1;
    else if (*s < 0xF0)
        expected = 2;
    else if (*s < 0xF8)
        expected = 3;
    else
        return 0;
    length = expected + 1;
    for (; expected; expected--)
        if (s[expected] < 0x80 || s[expected] >= 0xC0)
            return 0;
    return length;
}
原网站

版权声明
本文为[happytree001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140605113597.html