1

Trying to implementation of an absolute loader with input from : http://pastebin.com/k7VkA3xQ

Output currently obtained : http://pastebin.com/FiYPMWrZ

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <string.h>


int dec(char hex[]){ //function converts a hex array of characters to its equivalent decimal value

int length,i,sum=0;
for(length=0;hex[length]!='\0';length++);

for (i = 0;hex[i]!='\0'; ++i,--length)
{
    if (hex[i]>='0' && hex[i]<='9') 
        sum+=(hex[i]-'0')*pow(16,length-1);
    else if (hex[i]>='A' && hex[i]<='Z')    
        sum+=hex[i]-'A'+10;
    else if (hex[i]>='a' && hex[i]<='z')    
        sum+=hex[i]-'a'+10;
}
return sum;
}


int main(int argc, char const *argv[])
{
char record[2],progname[10],startaddr[10],proglen[10],addr[10],len[10];
int start,address,diff,flag=1,decimal;
FILE *fptr;

if (access("Input.dat",F_OK)!=-1)
{
    fptr = fopen("Input.dat","r");
    printf("File opened successfully\n");
}
else
{
    printf("File opened unsuccessfully\n");
    exit(1);
}


fscanf(fptr,"%s",record);
if (strcmp(record,"H")==0){
    fscanf(fptr,"%s",progname);fscanf(fptr,"%s",startaddr);fscanf(fptr,"%s",proglen);
}

fscanf(fptr,"%s",record);
while(strcmp(record,"T") == 0 ){
    fscanf(fptr,"%s",addr);fscanf(fptr,"%s",len);
    if (flag)
        sscanf(startaddr,"%x",&start);
    else
        start = address; //'start' fails to store the hex value of 'address'

    sscanf(addr,"%x",&address);
    diff = address - start;
        while((diff--)>0){
            printf("%x              xx\n",start++);
    }
    decimal = dec(len)%3==0?dec(len)/3:dec(len)/3+dec(len)%3;
    while(decimal--){
        fscanf(fptr,"%s",addr);
        printf("%x              ",address++);   printf("%c%c\n",addr[0],addr[1]);   
        printf("%x              ",address++);   printf("%c%c\n",addr[2],addr[3]);   
        printf("%x              ",address++);   printf("%c%c\n",addr[4],addr[5]);   
    }
        fscanf(fptr,"%s",record);
}


return 0;
}

Not sure of how to make 'start' store the value of 'address'...at present..'start' prints from 1000H ... kindly help me in storing the hexadecimal value 'address' in the int 'start'...input and output files for the above implementation have been attached above...thx!

mrdoubtful
  • 429
  • 2
  • 7
  • 17
  • 2
    A "hexadecimal number" isn't a number, it's a character string. On all standard computers a "number" is binary only. One may *represent* a number as a decimal string or a hexadecimal string (or as "two thousand five hundred and fortytwo", if you wish), but to the computer the *number* is still just binary bits, spelled out, coded as "2542", or as "0x9EE", it's all the same numeric value. – Hot Licks Jan 05 '15 at 22:16
  • You might consider `scanf` with the `%x` conversion, or `strtol` to handle the conversion. – Jerry Coffin Jan 05 '15 at 22:16
  • 1
    (Your algorithm above is simply summing the digits, BTW. To convert the hex representation to numeric you need to multiply `sum` by 16 ahead of each iteration.) – Hot Licks Jan 05 '15 at 22:19

1 Answers1

2

if the hexadecimal number is stored in a string hex with the form 0x... or 0X... or ... where points are representing digits in 16 base then you can use the sscanf() function to store the value in an integer number (decimalNumber for the example below) in 10 base

sscanf(hex,"%x",&decimalNumber);

So for a complete example

char hex[100]="0xf";
int decimalNumber;
sscanf(hex,"%x",&decimalNumber);
printf("The value of decimalNumber is %d\n",decimalNumber);

the output will be

The value of decimalNumber is 15