从读取函数中获取错误的十六进制值
Getting wrong hex value from read function
函数通过 1 步获取有效数据,但是当转到 2 步时获取错误的十六进制数据,我做错了什么?
stringstream getHexFromBuffer(char* buffer,short startFrom = 0, short to = 4)
{
int len = strlen(buffer);
stringstream hexValue;
for (size_t i = startFrom; i < to; i++) {
hexValue << hex << (int)buffer[i];
}
return hexValue;
}
bool big::openFile(std::string fName){
this->fileName = fName;
ifstream file(fName.c_str(), ios::binary | ios::out | ios::ate);
char* memblock;
this->fileSize = file.tellg();
memblock = new char[16];
file.seekg(0, ios::beg);
if (!file.is_open()) {
file.close();
throw new exception("Файл не может быть открыт!");
}
file.read(memblock, 16);
string bigCheck = hexToASCII(getHexFromBuffer(memblock).str());
if (strcmp(bigCheck.c_str(), "BIGF") != 0) {
throw new exception("Не .BIG архив!");
}
this->fileType = bigCheck.c_str();
string bigSize = getHexFromBuffer(memblock, 4, 8).str();
//cout << getIntFromHexString(bigSize) << endl << this->fileSize ;
file.close();
}
First valid attempt
Second not valid attempt
必须是 d0998100,但得到ffffffd0ffffff99ffffff810而不是
我试图得到的完整十六进制是 42 49 47 46 D0 99 81 00,也许它有帮助
d0
是 signed char 中的负 8 位值。这与 int 中的负值 ffffffd0
完全相同。为了在输出中获得“d0”,将有符号字符转换为另一个相同大小(1 字节)的无符号类型,然后将结果转换为 int:
hexValue << hex << (int)(unsigned char)buffer[i];
hexValue << hex << (int)(uint8_t)buffer[i];
第一个类型转换保留 8 位无符号类型,一个正数,第二个类型转换生成一个正整数。
函数通过 1 步获取有效数据,但是当转到 2 步时获取错误的十六进制数据,我做错了什么?
stringstream getHexFromBuffer(char* buffer,short startFrom = 0, short to = 4)
{
int len = strlen(buffer);
stringstream hexValue;
for (size_t i = startFrom; i < to; i++) {
hexValue << hex << (int)buffer[i];
}
return hexValue;
}
bool big::openFile(std::string fName){
this->fileName = fName;
ifstream file(fName.c_str(), ios::binary | ios::out | ios::ate);
char* memblock;
this->fileSize = file.tellg();
memblock = new char[16];
file.seekg(0, ios::beg);
if (!file.is_open()) {
file.close();
throw new exception("Файл не может быть открыт!");
}
file.read(memblock, 16);
string bigCheck = hexToASCII(getHexFromBuffer(memblock).str());
if (strcmp(bigCheck.c_str(), "BIGF") != 0) {
throw new exception("Не .BIG архив!");
}
this->fileType = bigCheck.c_str();
string bigSize = getHexFromBuffer(memblock, 4, 8).str();
//cout << getIntFromHexString(bigSize) << endl << this->fileSize ;
file.close();
}
First valid attempt
Second not valid attempt
必须是 d0998100,但得到ffffffd0ffffff99ffffff810而不是
我试图得到的完整十六进制是 42 49 47 46 D0 99 81 00,也许它有帮助
d0
是 signed char 中的负 8 位值。这与 int 中的负值 ffffffd0
完全相同。为了在输出中获得“d0”,将有符号字符转换为另一个相同大小(1 字节)的无符号类型,然后将结果转换为 int:
hexValue << hex << (int)(unsigned char)buffer[i];
hexValue << hex << (int)(uint8_t)buffer[i];
第一个类型转换保留 8 位无符号类型,一个正数,第二个类型转换生成一个正整数。