2

How I can convert in PE format of hex (for example "d43c23F1") to binary.

i use http://tomeko.net/online_tools/hex_to_file.php?lang=en, all characters outside sucess。

but use C++ program code fail。

my hex file code,code is "hello world" in C program:

4D5A90000300000004000000FFFF0000 B8000000000000004000000000000000 00000000000000000000000000000000 000000000000000000000000F0000000 0E1FBA0E00B409CD21B8014CCD215468 69732070726F6772616D2063616E6E6F 742062652072756E20696E20444F5320 6D6F64652E0D0D0A2400000000000000 D80EC51B9C6FAB489C6FAB489C6FAB48 My C program code:

#include "stdafx.h"
#include "StdAfx.h"
#include <stdlib.h>  
#include <string>  

int hex_to_int(char c)
{
    if (c >= 97)
        c = c - 32;
    int first = c / 16 - 3;
    int second = c % 16;
    int result = first * 10 + second;
    if (result > 9) result--;
    return result;
}

int hex_to_ascii(char c, char d) {
    int high = hex_to_int(c) * 16;
    int low = hex_to_int(d);
    return high + low;
}

int main()
{
    char str[16];
    // open file
    FILE* file = fopen("D:\\outside.exe", "a+");
    FILE *fp = fopen("D:/hex.txt", "rb+");
    fseek(fp, 0, SEEK_END);
    int fileLen = ftell(fp);
    char *tmp = (char *)malloc(sizeof(char) * fileLen);
    fseek(fp, 0, SEEK_SET);
    fread(tmp, fileLen, sizeof(char), fp);
    int length = strlen(tmp);
    int i;
    char buf = 0;
    for (i = 0; i < fileLen; i++) {
        if (i % 2 != 0) {
            printf("%c", hex_to_ascii(buf, tmp[i]));
            // result string to tmp 
            sprintf(str, "%c", hex_to_ascii(buf, tmp[i]));
            // to file end
            fseek(file, 0, SEEK_END);
            // if str  == '\0' , no data write to file
            fwrite(str, sizeof(char), 1, file);
        }
        else {
            buf = tmp[i];
        }
    }
    fclose(file);
    free(tmp);
    return 0;
}

compare binary result ,The contrast is due to improper handling of 0D0D

How to solve? picture is compare result

enter image description here

McMillan Cheng
  • 382
  • 1
  • 6
  • 20
boy hello
  • 21
  • 2

0 Answers0