WriteFile writes incorrect data

Hello,
I have a problem with the WriteFile function. The problem is the following:
I have an array of the type: char HEX[7];
When I debug it contains the following string: "FF0000".
The problem is when I use the WriteFile function it writes "00FF00" and not the correct string.
My function call is:
WriteFile(f, (LPCVOID)HEX, strlen(HEX), &bytes, NULL);

What am I doing wrong?

Regards,

Simon H.A.
The string "FF0000" is 3 bytes because a single byte requires 2 hexadecimal characters to be represented, so what you show there is not really correct.

But your problem relies in the fact that you are treating binary data as if it were a string. This is incorrect. Specifically, the strlen() function counts characters on a string until it reaches the first byte with value 0x00. If you want to write those zero-bytes (a. k. a. null chars), you must not use strlen() and instead use other methods like sizeof() (sizeof() would work because it is an array allocated in the stack).
Forgot to tell that it should be a string with the characters "FF0000". It's not a value.
It's for a program that converts a bitmap image to an array for your C/C++ project.

Regards,

Simon H.A.
This sounds like a bad diagnosis to me.

Your function call looks fine. I see no reason why incorrect data would be written, unless the data itself is bad at the time of writing (ie: hex does not actually contain "FF0000" when you are calling WriteFile).

Another possibility is that you're misreading the generated file somehow. "FF0000" is easy to mistake with "00FF00" when your file contains a bunch of other crap: "00000000FF00000000"
I found the problem.
It wasn't the problem anyways.
Thanks for your support,

Regards,

Simon H.A.
Topic archived. No new replies allowed.