c++ byte arrays how to output

Started by
3 comments, last by Bregma 11 years, 7 months ago
I have kind of a problem. This is the first time I have encountered a binary byte array and I need to output it to the screen.

typedef structure binary_byte_array{
UINT32 size;
UINT8 *data
}

Size is how many bytes are in the array.
*data is a pointer to the start of the data.


In the data is a file location. For example it will contain something like c:/something/somefilename.exe.

I need to be able to output the file location stored in the data. I know how to read binary data from files, but I am kind of stuck on how to read it stored in this kind of array. I am also confused how I would output it to the screen, do I need to convert it from binary to ascii char's?

The data is written in 4 byte blocks right? (The size of a UINT8) Or is that just the size of the pointer?

Thank you for any help. Microsoft uses this data type for some of their internal workings,
Advertisement
It depends on what the data contains.

If it contains text data with a string terminator you can print it to screen with the standard output functions.

If it does not contain text data you will need to do something special to handle non-printable characters. You could print them as their hex value, or do some fancy processing and turn it into whatever other display you want.

As for the data formats, UINT32 and UINT8 are just custom types they made for compatibility with different compilers. On most modern compilers you can use "unsigned long" and "unsigned char" if you don't want to use the UINT32 and UINT8.
All I know about the data is that it is stored as binary data.

A MSFT told me the data would contain /device/harddiskvolume2/something/something.exe

He never specified wether it was a null terminated string or char array or anything like that.

So does anyone have any assumptions?

In the MSDN documentation it is listed as FWP_BYTE_BLOB as the data type.
The byte blob is storing an AppId from the FWPM_NET_EVENT0 header.

If that helps.

How would I even output it with cout?

for(int a = 0; a < size; ++a)
{
cout << data + a;
}

?
Hi. improve your sample code:
for(int a = 0; a < size; ++a)
{
int p = *(data + a);// or int p = data[a];
cout << p;
}


for(int a = 0; a < size; ++a)
{
cout << data + a;
}

Try this, assuming the data is in fact a printable UTF-8 Pascal-style string.

for (int a = 0; a < size; ++a)
{
std::cout << *(data + a);
}
std::cout << "\n";

The quoted loop will print consecutive addresses. The second will print the characters at consecutive addresses. It will not work if the binary data is not either an ASCII string or some 8-but ASCII superset (ie. real UTF-* will not work for most international characters), but it's a good start.

A better way might be this.

std::string s(data, data+size);
std::cout << s << "\n";

which will convert the Pascal-style string into a C++-style string, which can be converted into a C-style string much more readily with the [font=courier new,courier,monospace]c_str()[/font] member function.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement