Improve company productivity with a Business Account.Sign Up

x
?
Solved

atoh ?

Posted on 2011-02-17
11
Medium Priority
?
1,951 Views
Last Modified: 2012-06-27
Hi, is where a funktion atoh ?

I need to get the hex value from a ascii string that include hex values.

hex_value = atoh(ascii_str);

Thanks

// Ludde72
0
Comment
Question by:Ludde72
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 3

Expert Comment

by:imaki06
ID: 34914808
What would be the type of the hex_value-variable?

If it is a string then try this:
char hex_value[33];
int i=atoi(ascii_str);
itoa(i,hex_value,16);
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34914815
try scanf

int i;
char str[]="abc";
scanf(str,"%x",&i);
print("%x",i); 

Open in new window

0
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 34914892
@imaki06: What you posted converts an integer generated from a decimal string into a hex string - as I understood the question is about the other direction, converting a hex string to an integer

@sjklein42: the function you mean is 'sscanf', nos 'scanf'

@Ludde72: Beside 'sscanf' you can even use 'strtol' to get the integer value from a hex string, i.e.:

int hex_value = strtol( ascii_str, 0, 16 );

You need to take care that the string contains only the hex data without a prefix like '0x'.

Hope that helps,

ZOPPO
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
LVL 33

Expert Comment

by:phoffric
ID: 34914907
itoa is not standard C. For standard ANSI C
see strtol : Convert string to long integer
or
strtoul : Convert string to unsigned long integer
This last link says: If the base is 16, the sequence may optionally be preceded by "0x" or "0X", which is ignored.
0
 
LVL 33

Expert Comment

by:phoffric
ID: 34914934
Hi Zoppo,
I just saw your comment about the prefix '0x' w.r.t. strtol.
Here is a quote from the strtol link:
"The sequence may optionally be preceded by a plus or minus sign and, if base is 16, an optional "0x" or "0X" prefix."
so maybe (as mentioned for strtoul), the "0x" or "0X", will also be ignored.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34914962
@phoffric: Thanks, I didn't remember this correctly - fine that you pointed this out ...
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34914986
Zoppo,

sscanf is C++

scanf is C

0
 
LVL 33

Expert Comment

by:phoffric
ID: 34915013
sscanf is C and C++ as well as scanf. Their functionality is different. See link:
    http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/
Sample code shows
#include <stdio.h>
a standard C library.
/* sscanf example */
#include <stdio.h>

int main ()
{
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i);
  printf ("%s -> %d\n",str,i);
  
  return 0;
}

Open in new window

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34915015
@sjklein42: This is not correct! 'stdio.h', which is the standard C input/output header (http://en.wikipedia.org/wiki/Stdio.h) declares these 'scanf' functions:

    int fscanf(FILE *stream, const char *format, ...);
    int scanf(const char *format, ...);
    int sscanf(const char *str, const char *format, ...);

So, this has nothing to do with C++.

Further, do you know a 'scanf' function in 'C' which takes it's input from a string instead of the standard input stream?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34915032
Zoppo,

You're right.  I meant sscanf.

http://msdn.microsoft.com/en-us/library/zkx076cy.aspx

Apologies
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 34915046
No problem :o)
0

Featured Post

Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
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 how to create, access, and change arrays in the C programming language.

584 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