• C

Hex to Ascii/Integer

(C on Solaris 2.x)
I'm having problems on how to store some Hex values.
Hopefully you can help this newbie in C.

First of all, here're 2 simple variables I'm having ...

      unsigned char *source, *dest;

After I read in a string e.g. "12AB" into source, I convert the string into Hex and put the output into dest :

        < ..read string "12AB" from file into source ...>          
          AsciiToHex(source, strlen(source), dest);

Now the problem is I need to store the decimal value of hex '12' (which is 1x16 + 2 = 18) and similarly that of 'AB' (which is 10x16+11=171) into 2 separate ascii strings :

           unsigned char *var1, *var2;

so that var1 contains "18" and var2 has "171" in ascii.

How do I do that ?

Thanks for helping !!!!!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

totoro030898Author Commented:
Edited text of question
Where do you allocate storage fot *dest?

unsigned char *source, *dest;
unsigned char *var1, *var2;
AsciiToHex(source, strlen(source), dest);
#include <string.h>

unsigned char xlate(char ch)
    if (ch >= '0' && ch <= '9')
        return ch - '0';
    else if (ch >= 'A' && ch <= 'F')
        return ch - 'A' + 10;
    else if (ch >= 'a' && ch <= 'f')
        return ch - 'a' + 10;
    else /* Error */
        return 0;

void AsciiToHex(char* src, int len, char* dest)
    if (len & 1) /* Odd length */
        *dest++ = xlate(*src++);

    while (len)
        *dest++ = xlate(*src++) * 16 + xlate(*src++);
        len -= 2;

    *dest = '\0';

void main()
    char from[] = "12A";
    char to[3];

    AsciiToHex(from, strlen(from), to);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
totoro030898Author Commented:
As a newbie to C, personally think that ozo's answer is short and simpler. however still thanks to alexo! i did learn something!
I gave you an implementation of AsciiToHex(), ozo gave you a way to use one.  Two different things.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.