C ++的二进制数据转换为十六进制字符串和背部 [英] C++ Converting binary data to a hex string and back
问题描述
我有一对匹配的一个工具类的静态函数,我使用二进制数据(无符号字符)之间的转换,它的字符串重新presentation(A-F和0-9)。他们似乎正常工作,但最近我试图编译我的code在Visual C ++(2010年防爆preSS)和我失望的是,它们会导致什么,但堆损坏错误。我在做什么错了?
I have a matching pair of static functions in a utility class that I use to convert between binary data (unsigned characters) and it's string representation (a-f and 0-9). They seemed to work correctly but recently I tried to compile my code under Visual C++ (2010 Express) and to my dismay, they cause nothing but heap corruption errors. What am I doing wrong?
void Utility::string_to_binary(const std::string source, unsigned char* destination, unsigned int length)
{
unsigned int effective_length = min(length, (unsigned int) source.length() / 2);
for(unsigned int b = 0; b < effective_length; b++)
{
sscanf(source.data() + (b * 2), "%02x", (unsigned int*) &destination[b]);
}
}
void Utility::binary_to_string(const unsigned char* source, unsigned int length, std::string& destination)
{
destination.clear();
for(unsigned int i = 0; i < length; i++)
{
char digit[3];
sprintf(digit, "%02x", source[i]);
destination.append(digit);
}
}
编辑:下面是说明该问题的完整方案
#include <iostream>
#include <hdcs/Utility.h>
using namespace std;
int main(int argc, char* argv[])
{
//Generate some data
unsigned int size = 1024;
unsigned char* data = new unsigned char[size];
//Convert it to it's string representation
string hex;
Utility::binary_to_string(data, size, hex);
//Output it to the screen
cout << hex << endl;
//Clear the data buffer
memset(data, 0, sizeof(unsigned char) * size);
//Convert the hex string back to binary
Utility::string_to_binary(hex, data, size);
//Cleanup
delete[] data;
}
在错误发生,删除[]数据
。
推荐答案
在此code,
for(unsigned int b = 0; b < effective_length; b++)
{
sscanf(source.data() + (b * 2), "%02x", (unsigned int*) &destination[b]);
}
您似乎写一个 unsigned int类型
的位置目标
,目的地+ 1
,目的地+ 2
,和C。当你接近你的目标
缓冲区的字节最后,你会写超出其极限。
you seem to be writing an unsigned int
at locations destination
, destination+1
, destination+2
, &c. As you approach the final bytes of your destination
buffer, you will write beyond its limit.
例如起见,我们假定目标是一个四字节的缓冲区,而的sizeof(unsigned int类型)
是在您的环境4。然后,每个的sscanf
正在写四个字节。
For the sake of example, let us assume that destination is a four-byte buffer, and that sizeof (unsigned int)
is 4 in your environment. Then each sscanf
is writing four bytes.
第一次迭代写入字节0,1,2,3
The first iteration writes bytes 0, 1, 2, 3
第二iteratino写入字节1,2,3,4
The second iteratino writes bytes 1, 2, 3, 4
第三迭代写字节2,3,4,5
The third iteration writes bytes 2, 3, 4, 5
的最终迭代写字节3,4,5,6
The final iteration writes bytes 3, 4, 5, 6
由于缓冲区是开始,你写超出你缓冲区末尾只有四个字节。热潮。
Since the buffer was only four bytes to start with, you have written beyond the end of your buffer. Boom.
修改
要避免这种特定的错误所需的最小变动如下:
The minimum change required to avoid this particular bug follows:
for(unsigned int b = 0; b < effective_length; b++)
{
unsigned int ui;
sscanf(source.data() + (b * 2), "%02x", &ui);
destination[b] = ui;
}
这篇关于C ++的二进制数据转换为十六进制字符串和背部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!