Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Hex to Ascii/Integer

Posted on 1998-05-20
5
Medium Priority
?
1,458 Views
Last Modified: 2012-05-04
(C on Solaris 2.x)
Hi,
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);
          dest[2]='\0';

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 !!!!!
0
Comment
Question by:totoro030898
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:totoro030898
ID: 1250869
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
ID: 1250870
Where do you allocate storage fot *dest?

unsigned char *source, *dest;
unsigned char *var1, *var2;
source=strdup("12AB");
dest=malloc((strlen(source)+3)/2);
AsciiToHex(source, strlen(source), dest);
var1=malloc(4);
var2=malloc(4);
sprintf(var1,"%d",dest[0]);
sprintf(var2,"%d",dest[1]);
0
 
LVL 11

Accepted Solution

by:
alexo earned 600 total points
ID: 1250871
#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++);
        len--;
    }

    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);
}

0
 

Author Comment

by:totoro030898
ID: 1250872
As a newbie to C, personally think that ozo's answer is short and simpler. however still thanks to alexo! i did learn something!
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250873
I gave you an implementation of AsciiToHex(), ozo gave you a way to use one.  Two different things.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question