Hex2Bin : Unable to convert multiple lines of hex

Hi All,

I have written a program to read hex from a txt file and to convert to binary and proceed to generate the result to a new txt file.

However, the program only able to convert the last line of the data.
I suspect it the data is not fully parsed.
I'm still at beginner-level, would appreciate on what I can do on the output conversion.
Thanks a lot.

My hex file:

1079FFFFB0F7FFFFFD13E7FFFEC1EC359FFFFF00
1079FFFFB0F7FFFFFD13E7FFFEC27DE6FFFFFF00
007FFFFF7037FFFFFD13E7FFFE7FFFFFFFFFFF00
007FFFFF7037FFFFFD13E7FFFE7FFFFFFFFFFF00


My result

****************************************************

This is a simple program to convert Hex to Binary.

****************************************************
Opening file for reading...
The file was opened successfully!
****************************************************

Hex numbers is :
1079FFFFB0F7FFFFFD13E7FFFEC1EC359FFFFF00

Hex numbers is :
1079FFFFB0F7FFFFFD13E7FFFEC27DE6FFFFFF00

Hex numbers is :
007FFFFF7037FFFFFD13E7FFFE7FFFFFFFFFFF00

Hex numbers is :
007FFFFF7037FFFFFD13E7FFFE7FFFFFFFFFFF00


Hex to binary value is :

00000000011111111111111111111111011100000011011111111111111111111111110100010011
11100111111111111111111001111111111111111111111111111111111111111111111100000000
Press any key to continue . . .



Code
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    char filename[] = "C://Users/ynchen/Desktop/Chen1.txt"; //Read file from user-defined address
    ifstream inputFile(filename, ios::in); //Test if fail to open file

    if(inputFile.fail()) //error handling for file opening
    {
        cout<<"Opening file for reading\n";
        cout<<"------------------------------------------\n";
        cout<<"The file could not be opened!\n";
        cout<<"Please name file as hex2bin in .txt file type\n";
        cout<<"Please make sure path is in C://hex2bin\n";

    }
    else //if file opened successfully, display success statement
    {
        cout<<"Opening file for reading...\n";
        Sleep(500);
        cout<<"The file was opened successfully!\n";
        cout<<"****************************************************\n";
       // return 0;

       while(inputFile.good())
        {
            getline(inputFile,hexbin);
          //  inputFile.read(buffer,10000);
            cout<<"\nHex numbers is : \n" << hexbin <<endl;

        }
    }
    inputFile.close();

    ofstream hexbinOutput("C://Users/ynchen/Desktop/hex2bin_result.txt");  //Create file at path

    Sleep(500); //Delay 0.5s

	cout<<"\n\nHex to binary value is : \n\n";

	while(hexbin[i])
	{

		switch(hexbin[i])
		{
			case '0' : cout<<"0000" ;
            hexbinOutput<< "0000";
				break;
			case '1' : cout<<"0001";
			hexbinOutput<< "0001";
				break;
			case '2' : cout<<"0010";
			hexbinOutput<< "0010";
				break;
			case '3' : cout<<"0011";
			hexbinOutput<< "0011";
				break;
			case '4' : cout<<"0100";
			hexbinOutput<< "0100";
				break;
			case '5' : cout<<"0101";
			hexbinOutput<< "0101";
				break;
			case '6' : cout<<"0110";
			hexbinOutput<< "0110";
				break;
			case '7' : cout<<"0111";
			hexbinOutput<< "0111";
				break;
			case '8' : cout<<"1000";
			hexbinOutput<< "1000";
				break;
			case '9' : cout<<"1001";
			hexbinOutput<< "1001";
				break;
			case 'A' : cout<<"1010";
			hexbinOutput<< "1010";
				break;
			case 'B' : cout<<"1011";
			hexbinOutput<< "1011";
				break;
			case 'C' : cout<<"1100";
                        hexbinOutput<< "1100";
				break;
			case 'D' : cout<<"1101";
			hexbinOutput<< "1101";
				break;
			case 'E' : cout<<"1110";
			hexbinOutput<< "1110";
				break;
			case 'F' : cout<<"1111";
			hexbinOutput<< "1111";
				break;
			case 'a' : cout<<"1010";
			hexbinOutput<< "1010";
				break;
			case 'b' : cout<<"1011";
			hexbinOutput<< "1011";
				break;
			case 'c' : cout<<"1100";
			hexbinOutput<< "1100";
				break;
			case 'd' : cout<<"1101";
			hexbinOutput<< "1101";
				break;
			case 'e' : cout<<"1110";
			hexbinOutput<< "1110";
				break;
			case 'f' : cout<<"1111";
			hexbinOutput<< "1111";
			case 32 : cout<<" \n";
			hexbinOutput<<" \n";
				break;
			default : cout<<" "<<hexbin[i];
		}
		i++;
	}

	hexbinOutput.close();
	system("pause");
	if(inputFile.fail())
    {
        cout<<"The file could not be closed!\n";
    }
    else
        cout<<"\nThe file is saved successfully and this program will close now, goodbye.\n";
}

Last edited on
Every time you read a new line, you overwrite the last one. Either do the 'hex to binary' bit each time you read a new line (ideally, throw it in a function and just call it), or store each line in a vector and then iterate over it.
Last edited on
Topic archived. No new replies allowed.