我在解析具有以下内容的二进制数据文件时遇到了一些麻烦,该文件读取了第一行
int LineLength = 300;
int Offset= 4586;
ifstream ifn("BinaryFile.bin");
char* line = new char[LineLength];
ifn.seekg(Offset);
while (ifn.good())
{
ifn.read(line, LineLength);
}
for (int i = 0; i < LineLength; i++)
printf("%02X ", line[i]);
这是打印出来的:
00 FFFFFFDD 01 00 00 22 FFFFFFB0 01 00 03 4F 50 2D 31 01 31 32 33
当我使用十六进制编辑器查看原始数据时,它看起来像这样:
00 55 24 00 00 F4 BC 01 00 03 4F 50 2D 31 01 31 32 33
我真的不希望它改变数据,我需要将一些字节转换为整数,一些转换为字符串,还有一些最终会变成日期,但我遇到了一些真正的挑战。
string* DataType= new string[3]{ "int","int","string" };
int* StartingPosition =new int[3] { 1,10,11 };
int* ColumnWidth =new int[3] { 4,1,25 };
char* locale = setlocale(LC_ALL, "UTF-8");
for (string Name : DataType) {
std::vector<char> CurrentColumnBytes(ColumnWidth);
for (int C = StartingPosition; C < ColumnWidth + StartingPosition; ++C)
{
int Index = C - StartingPosition;
CurrentColumnBytes[Index] = line[C];
}
if (DataType == "int") {
// still working out how to get a little endian result from 2-4 bytes if I use char
// since it is combining bytes as shown above
}
if (DataType == "string") {
for (std::vector<char>::const_iterator i = CurrentColumnBytes.begin(); i != CurrentColumnBytes.end(); ++i)
std::cout << *i;
}
}
我尝试使用wchar_t
andwint_t
而不是char
作为类型。我wint_t
能够将多个字节转换为一个小字节序 int,但是我似乎无法从中得到一个字符串。
对于它的价值,我还尝试使用以下方法来获取整数,但不能获取字符串。
int LineLength = 300;
int counter = 0;
wchar_t* line = new wchar_t[LineLength];
while ((c = fgetwc("BinaryFile.bin")) != WEOF) {
line[counter] = c;
counter++;
if (counter > LineLength - 1) {
break;
}
}