gpt4 book ai didi

c - 为什么多字节字符到 char32_t 的转换使用 UTF-8 作为多字节编码而不是特定于语言环境的编码?

转载 作者:行者123 更新时间:2023-12-04 15:35:52 24 4
gpt4 key购买 nike

我一直在尝试通过首先将接收到的输入转换为 char32_t 来将从 Big5 中的 Windows 命令提示符输入的汉字转换为 UTF-8。在 UTF-32 编码中,然后将其转换为 UTF-8。我一直在调用函数 mbtoc32来自 <uchar.h>完成这项工作,但它不断发送“编码错误”。

以下是我遇到的情况:

  • 将序列 (Big5) 转换为 wchar_t代表 mbstowcs成功了。
  • mbrtoc32将多字节序列作为 UTF-8,但语言环境不是。 (设置为 "",在我的机器上返回“Chinese (Traditional)_Hong Kong SAR.950”)

下面是我一直在编写的代码,试图调试我的问题,但没有成功。它尝试将“香”汉字(U+9999)转换为多字节表示,然后尝试将“香”(0xADBB)的Big5编码转换为wchar_tchar32_t .但是,从多字节 (Big5) 转换为 char32_t返回编码错误。 (矛盾的是,向mbrtoc32输入“香”的UTF-8序列确实成功返回0x9999)

#include <uchar.h>
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>

mbstate_t state;
int main(void){
setlocale(LC_CTYPE, "");
printf("Your locale is: %s\n", setlocale(LC_CTYPE, NULL));
char32_t chi_c = 0x9999;
printf("Character U+9999 is 香\n");
char *mbc = (char *)calloc(32, sizeof(char));
size_t mb_len;
mb_len = c32rtomb(mbc, chi_c, &state);
int i;
printf("The multibyte representation of U+9999 is:\n");
// 0xE9A699, UTF-8
for (i = 0; i < mb_len; i++){
printf("%#2x\t", *(mbc + i));
}
char *src_mbs = (char *)calloc(32, sizeof(char));
// "香" in Big5 encoding
*(src_mbs + 0) = 0xad;
*(src_mbs + 1) = 0xbb;
wchar_t res_wc;
mbtowc(&res_wc, src_mbs, 32); // Success, res_wc == 0x9999
char32_t res_c32;
mb_len = mbrtoc32(&res_c32, src_mbs, (size_t)3, &state);
// Returns (size_t)-1, encoding error
if (mb_len == (size_t)-1){
perror("Encoding error");
return errno;
}
else {
printf("\nThe 32-bit character representation of U+9999 is:\n%#x", res_wc);
}
return 0;
}

我还阅读了来自 cppreference.com 的文档,它说,

In any case, the multibyte character encoding used by this function is specified by the currently active C locale.

我希望 mbrtoc32表现得像mbtowc ,它将字符从特定于语言环境的编码转换为 UTF-32(在本例中为 Big5 为 UTF-32)。

有没有解决方案可以使用mbrtoc32将多字节字符转换为 char32_t没有“编码错误”?

附注:我在 Windows 10 上使用 Mingw-64,用 gcc 编译。

最佳答案

我发现了问题。我正在使用的 Mingw-w64 期望传递给 mbrtoc32c32rtomb 的所有多字节字符串都是 UTF-8 编码。

mbrtoc32 的代码:

size_t mbrtoc32 (char32_t *__restrict__ pc32,
const char *__restrict__ s,
size_t n,
mbstate_t *__restrict__ __UNUSED_PARAM(ps))
{
if (*s == 0)
{
*pc32 = 0;
return 0;
}

/* ASCII character - high bit unset */
if ((*s & 0x80) == 0)
{
*pc32 = *s;
return 1;
}

/* Multibyte chars */
if ((*s & 0xE0) == 0xC0) /* 110xxxxx needs 2 bytes */
{
if (n < 2)
return (size_t)-2;

*pc32 = ((s[0] & 31) << 6) | (s[1] & 63);
return 2;
}
else if ((*s & 0xf0) == 0xE0) /* 1110xxxx needs 3 bytes */
{
if (n < 3)
return (size_t)-2;

*pc32 = ((s[0] & 15) << 12) | ((s[1] & 63) << 6) | (s[2] & 63);
return 3;
}
else if ((*s & 0xF8) == 0xF0) /* 11110xxx needs 4 bytes */
{
if (n < 4)
return (size_t)-2;

*pc32 = ((s[0] & 7) << 18) | ((s[1] & 63) << 12) | ((s[2] & 63) << 6) | (s[4] & 63);
return 4;
}

errno = EILSEQ;
return (size_t)-1;
}

对于c32rtomb:

size_t c32rtomb (char *__restrict__ s,
char32_t c32,
mbstate_t *__restrict__ __UNUSED_PARAM(ps))
{
if (c32 <= 0x7F) /* 7 bits needs 1 byte */
{
*s = (char)c32 & 0x7F;
return 1;
}
else if (c32 <= 0x7FF) /* 11 bits needs 2 bytes */
{
s[1] = 0x80 | (char)(c32 & 0x3F);
s[0] = 0xC0 | (char)(c32 >> 6);
return 2;
}
else if (c32 <= 0xFFFF) /* 16 bits needs 3 bytes */
{
s[2] = 0x80 | (char)(c32 & 0x3F);
s[1] = 0x80 | (char)((c32 >> 6) & 0x3F);
s[0] = 0xE0 | (char)(c32 >> 12);
return 3;
}
else if (c32 <= 0x1FFFFF) /* 21 bits needs 4 bytes */
{
s[3] = 0x80 | (char)(c32 & 0x3F);
s[2] = 0x80 | (char)((c32 >> 6) & 0x3F);
s[1] = 0x80 | (char)((c32 >> 12) & 0x3F);
s[0] = 0xF0 | (char)(c32 >> 18);
return 4;
}

errno = EILSEQ;
return (size_t)-1;
}

这两个函数都希望给定的多字节字符串为 UTF-8,而不考虑语言环境设置。功能mbrtoc32c32rtombglibc简单地调用它们的宽字符对应物来转换字符。作为宽字符转换在 Mingw-w64 上正常工作,我使用 mbrtowcwcrtomb 替换 mbrtoc32c32rtomb分别喜欢glibc上的方式:

#include <uchar.h>
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>

mbstate_t state;
int main(void){
setlocale(LC_CTYPE, "");
printf("Your locale is: %s\n", setlocale(LC_CTYPE, NULL));
char *src_mbs = "\xad\xbb"; // "香" in Big5 encoding
char32_t src_c32 = 0x9999; // "香" code point
unsigned char *r_mbc = (char *)calloc(32, sizeof(char));
if (r_mbc == NULL){
perror("Failed to allocate memory");
return errno;
}
size_t mb_len = wcrtomb(r_mbc, (wchar_t)src_c32, &state); // Returns 0xADBB, Big5 of "香", OK
printf("Character U+9999 is %s, ( ", r_mbc);
for (int i = 0; i < mb_len; i++){
printf("%#hhx ", *(r_mbc + i));
}
printf(")\n");
// mb_len = c32rtomb(r_mbc, src_c32, &state); // Returns 0xE9A699, UTF-8 representation of "香", expected Big5
// printf("\nThe multibyte representation of U+9999 is:\n");
// for (i = 0; i < mb_len; i++){
// printf("%#hhX\t", *(r_mbc + i));
// }
char32_t r_c32 = 0;
// mb_len = mbrtoc32(&r_c32, src_mbs, (size_t)3, &state);
// Returns (size_t)-1, encoding error
mb_len = mbrtowc((wchar_t *)&r_c32, src_mbs, (size_t)3, &state); // Returns 0x9999, OK
if (mb_len == (size_t)-1){
perror("Encoding error");
return errno;
}
else {
printf("\nThe 32-bit character representation of U+9999 is:\n%#x", r_c32);
}
return 0;
}

关于c - 为什么多字节字符到 char32_t 的转换使用 UTF-8 作为多字节编码而不是特定于语言环境的编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59791872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com