Solved

I need a hex2str or hex2int function.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now