【发布时间】:2020-04-07 23:09:12
【问题描述】:
我正在尝试读取一个包含无符号字节的文件,并且我正在尝试将它们读取为 [0,255] 的整数范围。
当我查看扩展 ascii 表时,当我读到“┌”时,它等于 218,但我的程序取为 195 或 226,我不知道为什么。
这个问题也发生在很多扩展部分(超过128个)的字符上。
为什么我不能读取 ASCII 等价物,我该如何解决这个问题? 谢谢回复。。
这是我的代码,
int main()
{
unsigned int temp = 0;
int bytesread;
int fd = open("inputs.txt", O_RDONLY);
if(fd == -1)
{
printf("An error occured.. \n");
exit(-1);
}
else
{
bytesread = read(fd, &temp, 1);
}
printf("%d", temp);
return 0;
}
【问题讨论】:
-
你能提供文件的十六进制转储吗?
-
我认为你的文件不在 ascii 中,你的字符 "┌" 存储为 94e2 0a8c 而不是 da0a
-
@Ôrel:
0a是换行符,不是“┌”编码的一部分。 -
如果您使用的是 Unix 系统,请键入
od -xa inputs.txt以查看文件中十六进制的实际字节,或键入od -tuC -a inputs.txt以查看十进制的字节。如果文件很大,使用od -tuC -a inputs.txt | more来控制输出。您可能会看到文件中的字节不是您想的那样。 -
没有理由
unsigned int temp = 0;和read(fd, &temp, 1)。推荐unsigned char temp = 0;解决这个问题。
标签: c file byte ascii extended-ascii