-3

I build this function that take a char with a hex value and return the value as an integer, the hex char value can star with 0x or 0X and could has lower or upper char, you must check if is a correct hex value if not return -1.

I am asking if you can improve this code or do it in a more efficient way, I can't use c or another library just use the c core. Just c no c++.

long long unsigned int atoi_hexadecimal(char s[])
{
    long long unsigned int num = 0;
    long long unsigned int size = 0;
    long long unsigned i=0;

    while(s[i]!='\0')
        i++;
    size=i;


    for(i=0 ; (((s[i]>='0') && (s[i]<='9')) || ((s[i]>='A') && (s[i]<='F')) || ((s[i]>='a') && (s[i]<='f')) || ((s[i]=='x') || (s[i]=='X'))); ++i)
    {
        if((i==0) && (s[i]!='0'))
            return -1;
        else if ((i==1) && (s[i]!='x') && (s[i]!='X'))
            return -1;
        else if ((s[i]>='A') && (s[i]<='F'))
                num = num + (s[i]-'A'+10)*power(16,size-i-1);
        else if ((s[i]>='a') && (s[i]<='f'))
                num = num + (s[i]-'a'+10)*power(16,size-i-1);
        else if ((s[i]>='0') && (s[i]<='9') && (i!=0))
                num = num + (s[i]-'0')*power(16,size-i-1);
        else if ((i!=0) && (s[i]!=0) && (s[i]!='x') && (s[i]!='X'))
            return -1;
    }

    if(i==size)
        return num;
    else return -1;
}

double power(double base, double exp)
{
    double p=1;

    for(int i=0; i<exp; i++)
    {
        p=p*base;
    }

    return p;
}

Example of use

printf("%llu \n",atoi_hexadecimal("0XF15"));

Can anybody improve this code for transform char that contain a hex to int value or do it in another way?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Edgar Gomez
  • 145
  • 1
  • 1
  • 10
  • 2
    You declare this `long long unsigned int atoi_hexadecimal` and then you do this `return -1;`. Are you sure you know what are you doing there? – Michi Apr 06 '17 at 21:29
  • Those `else if` where you have there `num = num + (s[i]-'A'+10)*power(16,size-i-1);` you do realize that you have a conversion problem in there.... – Michi Apr 06 '17 at 21:32
  • One last thing `double power(double base, double exp);`, why did you call it `power` ? – Michi Apr 06 '17 at 21:34
  • 1
    And the (very) last thing, `Can anybody improve this code`, why should someone here on SO waste their time for you?? – Michi Apr 06 '17 at 21:35
  • i just looking to improve my code, what some one would waste time for me? because is a site for answer question if you don't wanna answer then don't do it i am not paying for this. Thanks for the advices, i would like to see another opinions from other programmers – Edgar Gomez Apr 06 '17 at 21:46
  • num = num + (s[i]-'A'+10)*power(16,size-i-1); the conversion problem is because the power function return? i Called power because math called pow just fast tip, the long long unsigned is because hex can hold bigger integers with a shorter syntax – Edgar Gomez Apr 06 '17 at 21:50
  • `(s[i]!=0)` --> `(s[i]!='0')` ? – BLUEPIXY Apr 06 '17 at 21:53
  • We are not a coding or code review service. – too honest for this site Apr 06 '17 at 21:54
  • @Michi Well, people are doing this on SE network. It's just not the right one. – Eugene Sh. Apr 06 '17 at 22:03
  • Well, I would be tempted to use a [256] const byte lookup table to convert the hex char to nibble or error, (0xFF). Lookup, check, shift, lookup, check, shift... – ThingyWotsit Apr 06 '17 at 22:06
  • `long long unsigned int size = 0; long long unsigned i=0; while(s[i]!='\0') i++; size=i;` would be better replaced by `size_t size = strlen(s); unsigned long long int i = 0;`. I have reservations about `unsigned long long int` in use for indexes, if only because `size_t` is more compact and at least as correct. And you shouldn't need to use `double` values to produce your answer — use integer arithmetic only. – Jonathan Leffler Apr 06 '17 at 22:23

2 Answers2

1
long int strtol(const char *str, char **endptr, int base)

will help you

Look this and this

just make sure that your input is correct and go on

Community
  • 1
  • 1
bobra
  • 615
  • 3
  • 18
0

there are functions for that if you #include <stdlib.h>. Put in 16 as the radix.

Gold Dragon
  • 480
  • 2
  • 9