Holding NUL char

Hello again guys

So what I'm trying to achieve is next:
- Read file for certain number of bytes until EOF
- Convert each chunk to hexadecimal using function Str2Hex

Code is working well, only one problem pops up and it is handling NUL ("\0") byte. When "\0" byte is detected i convert it like this buffer[i] = "0" but problem occurs when Str2Hex try to convert that to HEX value, it becomes "20" but what i need is NUL byte which is presented as "00" in HEX value, to achive "00" in hex, buffer should be defined as buffer[i] = 0 but it truncates rest of data after that. I'm currently out of ideas for solutions, i tried many things but anything cant get over my head now. If someone faced this problem earlier any advice would be more than welcome!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const char* Str2Hex(const char *str, char *hex, size_t maxlen)
{
	static const char* const lut = "0123456789ABCDEF";

	if (str == NULL) return NULL;
	if (hex == NULL) return NULL;
	if (maxlen == 0) return NULL;

	size_t len = strlen(str);

	char *p = hex;

	for (size_t i = 0; (i < len) && (i < (maxlen - 1)); ++i)
	{
		const unsigned char c = str[i];

		*p++ = lut[c >> 4];
		*p++ = lut[c & 15];
	}

	*p++ = 0;

	return hex;
}

void _fileRead(char *fPath) {
	HANDLE fCreate;
	DWORD  bytesRead;

	fCreate = CreateFile(fPath, GENERIC_READ, FILE_SHARE_READ, NULL, 3, FILE_ATTRIBUTE_NORMAL, NULL);

	char    *buffer = new char[2049]{};
	char    hexBuff[((2048 * 2) + 1)];

	SetFilePointer(fCreate, 0, FILE_BEGIN, NULL);

	DWORD reader = 0;
	DWORD fSize = 0;
	fSize = GetFileSize(fCreate, NULL);


	while(reader < fSize) {

		DWORD finalChunkSize = 0;

		if ((reader + finalChunkSize) > fSize) {
			finalChunkSize = (fSize - reader);
		}
		else {
			finalChunkSize = 2048;
		}

		char    *buffer = new char[finalChunkSize + 1]{};

		ReadFile(fCreate, buffer, 2048, &bytesRead, NULL);

		reader += finalChunkSize;

		if (bytesRead == 0) {
			MessageBox(NULL, "EOF detected", "EOF Detection", MB_OK);
			delete[] hexBuff;
			break;
		}

		for (int i = 0; i < 2048; i++)
		{
			if (buffer[i] == '\0')
			{
				buffer[i] = '0'; // Here I'm not sure how to save it in buffer so leater when i try to convert it to hex result should be "00".
			}

		}		

		const char *result = Str2Hex(buffer, hexBuff, strlen(hexBuff));

		MessageBox(NULL, result, "OK", MB_OK);
		Sleep(2000);

		delete[] buffer;
	}
}
strlen reads the string until it reaches null.

You should put the size of the string inside of bytesRead into the parameter if it is not a null terminating string.

I noticed that you already asked this question before here http://www.cplusplus.com/forum/beginner/242839/

And JLBorges gave a much better solution than the one I was about to submit, I was gonna use an ifstream(binary flag), to a vector, to a formatted stringstream. Which probably is probably a bit slower, but note that you still haven't finished your error checking in your code.

Even if I had a knife to my neck telling me to use only windows API with absolutely no c++ standard library, I would just wrap all the functionality into classes and let the constructor and destructor and functions do all the manual memory management and error checking for me, getting rid of copy-paste code, making it more readable and safe. And even if I did that, what have I accomplished? I just reinvented the wheel.

(but for a beginner that could be a nice learning experience).

Your code is 100% compatible with C code, so if that is your goal you can get an honest answer in C on the cboard.cprogramming.com website. (but I wouldn't be surprised if they told you to use fopen + strcpy(buf,"%X",c) instead of the windows API :p)
Last edited on
Topic archived. No new replies allowed.