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;
}
}
|