将 ifstream 读取到 char* 以进行 int 和字符串转换
Reading ifstream to char* for both int and string conversion
我在解析二进制数据文件时遇到一些问题,该文件读取第一行
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
和wint_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;
}
}
好的,所以问题是您需要将文件读取到unsigned char vector
/数组中。也不要使用fgetwc
否则你只会得到每隔一个字节,它会检测一行中何时有多个空字节并将它们组合在一起,这可能会抛弃你的计数。Ifstream
似乎没有一种简单的方法可以将文件的一部分读取到unsigned char vector
int LineLength = 300;
int counter = 0;
std::vector<unsigned char> line(LineLength);
while ((c = fgetw("BinaryFile.bin")) != WEOF) {
line[counter] = static_cast<unsigned char>(c);
counter++;
if (counter > LineLength - 1) {
break;
}
}
然后,一旦将其放入向量中,您就可以单独处理数据
for (string Name : DataType) { // here just looping over the columns that were defined in the JSON file - excluding some things that aren't relevant
{
char* locale = setlocale(LC_ALL, "UTF-8");
std::vector<unsigned char> CurrentColumnBytes(ColumnWidth);
for (int C = StartingPosition; C < ColumnWidth + StartingPosition; ++C)
{
int Index = C - StartingPosition;
CurrentColumnBytes[Index] = BufferedLine[C];
}
if (DataType == "int") {
cout << DataParser::LEByte(CurrentColumnBytes); //custom function that tests length, converts to Little Endian int from the individual bytes.
}
if (DataType == "string") {
for (int i = 0; i < ColumnWidth; i++)
cout<<CurrentColumnBytes[i];
}
cout<<" | ";
}
相关文章:
- 我如何获取从 C++ 到 C 的映射<字符串、int>#
- 构造<int>具有 2 个字符串文字的向量
- 字符串中int的加法运算
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- cin >> int 给定一个字符串将 int 赋值为 0
- 将字符串中的 int 转换为字母;
- 使用 switch,但我想用字符串而不是 int 进行选择
- 两个数组,int 和 string,如何显示带有对应 int 的字符串?
- 从字符串流中读取具有特定字段宽度的 int
- win 表单应用程序字符串^ 到 int
- C++程序在将 int 与 cin 一起使用时有效,但不能使用字符串
- 将子字符串字符从字符串值转换为 int,然后将其分配给 int 变量
- 将字符串转换为 int 后的随机值
- 如何检查字符串是否正确以将其转换为 int?
- 在乌鸦中获取非Int(字符串)URL资源ID
- 如何使 int 字符串中的每个输入都成为一位数
- 将无符号int+字符串强制转换为无符号char向量
- 如何在C++中连接字符串+int+字符串
- 如何在消息框中使用int字符串?
- 如何在C++中将向量转换为<int>字符串