problem in HExadecimal to decimal conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rupali12345
    New Member
    • Jun 2007
    • 13

    problem in HExadecimal to decimal conversion

    Hi all,

    Code:
          int main() 
           { 
       		FILE *fp1;
    		char *c, *d;
    		c  = new char[54];
    		d = c;
    	                  fp1=fopen("C:\\xyz.dcm","r");
    		   for(int i=128;i<=163;i++)
    		{
      		  fseek(fp1,i,SEEK_SET);
    		  *d =(char )fgetc(fp1);
    		  d++;
    		  cout<<*c++;
    		}
    		for(int j=164;j<=166;j++)
    		{
    			
    		  fseek(fp1,j,SEEK_SET);
    		   cout <<dec <<  fgetc(fp1) <<endl;
     		}
    	  
    	     return 0;		
    
          }
    in this code, 164th byte from file is 1a. it is displaying as -1 instead of 26 ie decimal value.
    what should i have to do to get 26 as a decimal value of 1a????
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    How are you getting 1a in a byte? 1a is two bytes. If have 26 in byte 164 then is should display 26. fgetc() returns an int so << fgetc() should show 26. A char with 26 is not displayable.

    BTW, you should be using ifstream instead of the C file functions.

    Comment

    Working...