Solved

I need a hex2str or hex2int function.

Posted on 1998-07-07
5
2,580 Views
Last Modified: 2012-05-04

Hi,

I am in need of a function to transform hex strings (for ex: "1cf3bei4") to their
equivalent strings or unsigned long int.

Thank you.
0
Comment
Question by:zackeel
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1251544
scanf("1cf3bei4","%lx",&i);
i=strtoul("1cf3bei4",(char **)NULL,16);
0
 
LVL 1

Accepted Solution

by:
vladimir_12345 earned 100 total points
ID: 1251545
Hi! It's simple:

#include <stdio.h>

long HexStr2Long(const char* szHexStr)
{
     unsigned long Result;
     sscanf(szHexStr, "%lx", &Result);
     return Result;
}

char* HexStr2DecStr(const char* szHexStr, char* szDecStr)
{
     sprintf(szDecStr, "%ld", HexStr2Long(szHexStr));
     return szDecStr;    
}

0
 
LVL 1

Expert Comment

by:vladimir_12345
ID: 1251546
Hi! A little error:
use        unsigned long HexStr2Long(const char* szHexStr)
instead of          long HexStr2Long(const char* szHexStr)
 

0
 
LVL 11

Expert Comment

by:alexo
ID: 1251547
ozo, why do you insist on posting perfectly good answers as comments?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1251548
Well, I actually seem to have made a typo of scanf for sscanf,
But I figure it's up to the questioner to determine what's a perfectly good answer,
and I'd rather err on the safe side than have a reputation of posting
perfectly poor comments as answers.
And in this case, I didn't think I could give a complete answer,
not knowing what zackeel meant by «equivalent strings» for "1cf3bei4"
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

792 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