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

x
?
Solved

I need a hex2str or hex2int function.

Posted on 1998-07-07
5
Medium Priority
?
2,646 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
[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
 
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 200 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

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

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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

636 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