Solved

I need a hex2str or hex2int function.

Posted on 1998-07-07
5
2,568 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

770 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