判断字符串是否为utf-8编码(c语言)

本文分享了一段有效的UTF-8编码检测代码,通过逐字节解析,判断字符串是否符合UTF-8编码规则。深入探讨了UTF-8编码机制,包括1到6字节编码范围及错误检测逻辑。

先上代码,实测有效:

bool utf8_check(const char* str,size_t length)
{
    size_t i = 0;
    int nBytes = 0;////UTF8可用1-6个字节编码,ASCII用一个字节
    unsigned char ch = 0;
    while(i < length)
    {
        ch = *(str + i);
        if(nBytes == 0)
        {
            if((ch & 0x80) != 0)
            {
                while((ch & 0x80) != 0)
                {
                    ch <<= 1;
                    nBytes ++;
                }
                if((nBytes < 2) || (nBytes > 6))
                {
                    return false;
                }
                nBytes --;
            }
        }
        else
        {
            if((ch & 0xc0) != 0x80)
            {
                return false;
            }
            nBytes --;
        }
        i ++;
    }
    return (nBytes == 0);
}

详述字符编码:

https://blog.youkuaiyun.com/caixiaobai_1/article/details/103876783

在 C 语言中使用 UTF-8 编码发送字符串,需要明确处理字符串编码方式,并确保发送端与接收端采用相同的字符集标准。C 语言本身不提供内置的字符编码转换机制,因此通常依赖手动操作或第三方库来实现 UTF-8 编码解码。 ### 使用 UTF-8 编码发送字符串 在 Windows 平台上,可以使用 `MultiByteToWideChar` `WideCharToMultiByte` 函数进行编码转换;而在 Linux 或其他类 Unix 系统中,则可以借助 `iconv` 库进行字符集转换。以下是一个使用 `iconv` 的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iconv.h> int convert_encoding(const char *from_charset, const char *to_charset, const char *input, size_t in_len, char *output, size_t out_len) { iconv_t cd = iconv_open(to_charset, from_charset); if (cd == (iconv_t)-1) { perror("iconv_open"); return -1; } char *inbuf = (char *)input; char *outbuf = output; size_t inbytesleft = in_len; size_t outbytesleft = out_len; if (iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft) == (size_t)(-1)) { perror("iconv"); iconv_close(cd); return -1; } *outbuf = '\0'; iconv_close(cd); return 0; } int main() { const char *utf16_str = u8"你好,世界!"; // 假设为 UTF-16 或宽字符 char utf8_str[256]; // 将 UTF-16 转换为 UTF-8(实际中根据具体输入编码调整) if (convert_encoding("UTF-16", "UTF-8", utf16_str, strlen(utf16_str), utf8_str, sizeof(utf8_str)) == 0) { printf("UTF-8 Encoded String: %s\n", utf8_str); // 发送 UTF-8 字符串 // send(socket_fd, utf8_str, strlen(utf8_str), 0); } return 0; } ``` 该程序展示了如何将一个非 UTF-8 编码字符串转换为 UTF-8 格式,并准备通过网络或其他方式发送出去[^1]。 ### 接收端解码 UTF-8 字符串 接收端必须以相同的 UTF-8 编码方式进行解码,才能正确还原原始文本内容。例如,若接收到的数据是 UTF-8 编码的字节流,则可直接作为字符串处理,或者再次使用 `iconv` 进行转换: ```c // 接收 UTF-8 数据并验证是否符合 UTF-8 编码规则 int is_valid_utf8(const char *str, size_t length) { size_t i; int nBytes = 0; unsigned char chr; for (i = 0; i < length; i++) { chr = str[i]; if (nBytes == 0) { if ((chr & 0x80) != 0) { while ((chr & 0x80) != 0) { chr <<= 1; nBytes++; } if (nBytes < 2 || nBytes > 6) return 0; nBytes--; } } else { if ((chr & 0xC0) != 0x80) return 0; nBytes--; } } return (nBytes == 0); } int main() { const char *received_data = "\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81"; if (is_valid_utf8(received_data, strlen(received_data))) { printf("Received valid UTF-8 string: %s\n", received_data); } else { printf("Invalid UTF-8 encoding.\n"); } return 0; } ``` 上述函数实现了 UTF-8 编码格式的校验功能,有助于判断接收到的数据是否为合法的 UTF-8 字符串[^2]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值