gpt4 book ai didi

c - C 中的 UTF-32 到 UTF-8 转换器,缓冲区充满空/零

转载 作者:行者123 更新时间:2023-11-30 14:28:44 25 4
gpt4 key购买 nike

我一直在努力让它发挥作用。该程序应该采用两个参数,一个用于缓冲区大小,另一个用于文件名,并将该文件从 UTF-32 转换为 UTF-8。我一直在使用 fgetc() 函数用 Unicode 代码点填充 int 数组。我已经测试了 printint 输出缓冲区的内容,它具有所有这些空字符而不是每个代码点。

例如,对于仅包含字符“A”的文件:缓冲区[0]为0缓冲区[1]为0缓冲区 [2] 为 0缓冲区 [3] 为 41

U+7F 以上的任何代码点最终都会被分开。

这是初始化缓冲区的代码:

int main(int argc, char** argv) {
if (argc != 3) {
printf("Must input a buffer size and a file name :D");
return 0;
}

FILE* input = fopen(argv[2], "r");
if (!input) {
printf("The file %s does not exist.", argv[1]);
return 0;
} else {
int bufferLimit = atoi(argv[1]);
int buffer[bufferLimit];
int charReplaced = 0;
int fileEndReached = 0;
int i = 0;
int j = 0;

while(1) {
// fill the buffer with the characters from the file.
for(i = 0; i < bufferLimit; i++){
buffer[i] = fgetc(input);
// if EOF reached, move onto next step and mark that
// it has finished.
if (buffer[i] == EOF) {
fileEndReached = 1;
break;
}
}
// output buffer of chars until EOF or end of buffer
for(j = 0; j <= i; j++) {
if(buffer[j] == EOF) {
break;
}
// check for Character Replacements
charReplaced += !convert(buffer[j]);
}
if(fileEndReached != 0) {
break;
}
}
//return a 1 if any Character Replacements were used
if(charReplaced != 0) {
return 1;
}
}
}

最佳答案

fgetc() 返回一个字节,而不是 unicode 代码点。

从那时起,基于这个错误的假设,整个事情就崩溃了。

关于c - C 中的 UTF-32 到 UTF-8 转换器,缓冲区充满空/零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5609110/

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