(string subscript out of range )error

My code is to read a file of hex numbers then to convert them to binary .
First problem :
that it reads the lines and convert them to binary but the last digit of the first line is not converted !!
Ex:

hex no :
023F8 // 8 won't be converted ??
11245
0223A

bin no:
0000001000111111
00010001001001000101
00000010001000111010

Second problem :
The file that i read from it the hex numbers contain some comments that we must read them and print them out in the screen, you can notice that i interested in
keeping those comments not converted, but it doesn't work ??

The File Content :

!!
!!
!!
!! This is a comment line
!! START = 00100
!! LAST = 00118
!! CODE
!! LSB_...._MSB
!! instructions
032018
6B2015
0B2012
6F200F
77200C
072009
1B2006
2B2003
0F2003
000001





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
  // ConsoleApplication8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<cmath>
#include<cstdlib>

using namespace std; 
 
 class hextobin 
 {
 private:

 public:
	 void convh_b(string);
	 

 };

 void hextobin::convh_b(string x)
 {   
	 for(int i=0;i<6;i++)
	 { 
		 
          switch (x[i])
           {   
           	 case '0' : cout<<"0000"; break;
	         case '1':cout<<"0001";break;
	         case '2':cout<<"0010";break;
	         case '3':cout<<"0011";break;
	         case '4':cout<<"0100";break;
	         case '5':cout<<"0101";break;
	         case '6':cout<<"0110";break;
	         case '7':cout<<"0111";break;
	         case '8':cout<<"1000";break;
                 case '9':cout<<"1001";break;
	         case 'A': cout<<"1010";break;
	         case 'B':cout<<"1011";break;
           	 case 'C':cout<<"1100";break;
	         case 'D':cout<<"1101";break;
	         case 'E':cout<<"1110";break;
	         case 'F':cout<<"1111";break; 
           }
	 }
 cout<<endl; 
 
 }


void main() 

{   int i =0; 
	hextobin p;
    string  l1;
	ifstream myfile ("assembly.txt");
	if(myfile.is_open())
	{   
		while(!myfile.eof())
		{ 
		getline (myfile,l1); 
		cout<<l1<<endl;
		i++; 
		if (i>9 && i<20 )
		{
		p.convh_b(l1);
		} 
		
        }
		myfile.close(); 
	
	}
	else cout<< "unable";
}

At line 25,
 
    for(int i=0;i<6;i++)

instead of assuming 6 characters in the string, use the actual length
 
    for (int i=0; i<x.size(); i++)


While we're talking about this code, a couple of other thoughts.
Instead of this:
61
62
63
    while(!myfile.eof())
    { 
        getline (myfile,l1);

do this, it is less error prone:
61
62
    while(getline (myfile,l1))
    {         


As for the comment lines in the file, I suggest you identify those by checking for "!!" as the first two characters ( be careful if the line has less than two characters).
Topic archived. No new replies allowed.