Visual c++ 为什么';t unsigned char*是否使用ifstream::read? 我是一个C++初学者。我在工作中有一个新项目,我必须学习它,所以我正在尝试一些东西来测试我的理解力。对于这个问题,我尝试读取一个文件,然后在屏幕上打印它。超级简单,只是想熟练掌握它并理解我正在使用的函数。我将MS Word文档中的一些文本复制到记事本(*.txt)文件中,并尝试读取此*.txt文件。word文档中的所有文本均为粗体,但除此之外,没有“异常”字符。除粗体“-”符号外,所有内容均按文档中显示的方式打印在屏幕上。此字符打印为带帽字符的“u”(“扩展ASCII”代码150)。我试图在数组中打印出这个字符的整数值(应该是150),但是得到了-106。我意识到这个有符号整数的位与无符号整数150的位相同。我的问题是如何将输出设置为150?这是我的密码: #include <iostream> #include <fstream> using namespace std; int main() { unsigned char* input1; int input1size = 57; ifstream file("hello_world2.txt",ios::binary | ios::ate); if (file.is_open()){ int size; size = (int) file.tellg(); cout <<"This file is " << size << " bytes." << endl; file.seekg(0,ios::beg); input1 = new unsigned char[input1size]; file.read(input1, input1size); cout << "The first " << input1size <<" characters of this file are:" << endl<<endl; for (int i=0; i<input1size; i++) { cout << input1[i]; } cout<<endl; } else { cout <<"Unable to open file" << endl; int paus; cin>>paus; return 0; } file.close(); int charcheck = 25; int a=0; int a1=0; int a2=0; unsigned int a3=0; unsigned short int a4=0; short int a5=0; a = input1[charcheck]; a1 = input1[charcheck-1]; a2 = input1[charcheck+1]; a3 = input1[charcheck]; a4 = input1[charcheck]; a5 = input1[charcheck]; cout <<endl<<"ASCII code for char in input1[" << charcheck-1 <<"] is: " << a1 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] is: " << a << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck+1 <<"] is: " << a2 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned int: " << a3 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned short int: " << a4 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as short int: " << a5 << endl; int paus; cin>>paus; return 0; }

Visual c++ 为什么';t unsigned char*是否使用ifstream::read? 我是一个C++初学者。我在工作中有一个新项目,我必须学习它,所以我正在尝试一些东西来测试我的理解力。对于这个问题,我尝试读取一个文件,然后在屏幕上打印它。超级简单,只是想熟练掌握它并理解我正在使用的函数。我将MS Word文档中的一些文本复制到记事本(*.txt)文件中,并尝试读取此*.txt文件。word文档中的所有文本均为粗体,但除此之外,没有“异常”字符。除粗体“-”符号外,所有内容均按文档中显示的方式打印在屏幕上。此字符打印为带帽字符的“u”(“扩展ASCII”代码150)。我试图在数组中打印出这个字符的整数值(应该是150),但是得到了-106。我意识到这个有符号整数的位与无符号整数150的位相同。我的问题是如何将输出设置为150?这是我的密码: #include <iostream> #include <fstream> using namespace std; int main() { unsigned char* input1; int input1size = 57; ifstream file("hello_world2.txt",ios::binary | ios::ate); if (file.is_open()){ int size; size = (int) file.tellg(); cout <<"This file is " << size << " bytes." << endl; file.seekg(0,ios::beg); input1 = new unsigned char[input1size]; file.read(input1, input1size); cout << "The first " << input1size <<" characters of this file are:" << endl<<endl; for (int i=0; i<input1size; i++) { cout << input1[i]; } cout<<endl; } else { cout <<"Unable to open file" << endl; int paus; cin>>paus; return 0; } file.close(); int charcheck = 25; int a=0; int a1=0; int a2=0; unsigned int a3=0; unsigned short int a4=0; short int a5=0; a = input1[charcheck]; a1 = input1[charcheck-1]; a2 = input1[charcheck+1]; a3 = input1[charcheck]; a4 = input1[charcheck]; a5 = input1[charcheck]; cout <<endl<<"ASCII code for char in input1[" << charcheck-1 <<"] is: " << a1 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] is: " << a << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck+1 <<"] is: " << a2 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned int: " << a3 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as unsigned short int: " << a4 << endl; cout <<endl<<"ASCII code for char in input1[" << charcheck <<"] as short int: " << a5 << endl; int paus; cin>>paus; return 0; },visual-c++,Visual C++,所以看起来“int a”总是被解读为有符号的。当我尝试使“a”无符号时,它将字符的8位中的所有剩余位都转换为1。为什么会这样?对不起,问题太长了,我只是想说得详细一点。谢谢 您要处理的是,当您将字符分配给某个a时,字符被提升为int时发生的符号扩展?变量 所有高阶位必须设置为1,以使其保持与较小的字符存储中相同的负值。那么有没有办法输出无符号值150?当我试图将指针input1声明为“unsigned char*input1;”(正如在原始帖子的代码中所发布的那样)时,我得到一个错误,错误是……

所以看起来“int a”总是被解读为有符号的。当我尝试使“a”无符号时,它将字符的8位中的所有剩余位都转换为1。为什么会这样?对不起,问题太长了,我只是想说得详细一点。谢谢

您要处理的是,当您将字符分配给某个a时,字符被提升为int时发生的符号扩展?变量


所有高阶位必须设置为1,以使其保持与较小的字符存储中相同的负值。

那么有没有办法输出无符号值150?当我试图将指针input1声明为“unsigned char*input1;”(正如在原始帖子的代码中所发布的那样)时,我得到一个错误,错误是……::read“无法将参数1从unsigned char*”转换为“char*”。是的。使用掩码:
a3=input1[charcheck]&0xff
删除符号扩展一位。那么你的电脑将显示150。啊,好的,谢谢!目前我对面具一无所知,但现在我有了一个搜索方向。我感谢你的帮助!
    This file is 80 bytes.
    The first 57 characters of this file are:

    STATUS REPORT
    PERIOD 01 u 31 JUL 09

    TASK 310: APPLIC

    ASCII code for char in input1[24] is: 32
    ASCII code for char in input1[25] is: -106
    ASCII code for char in input1[26] is: 32
    ASCII code for char in input1[25] as unsigned int: 4294967190
    ASCII code for char in input1[25] as unsigned short int: 65430
    ASCII code for char in input1[25] as short int: -106