• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1922
  • Last Modified:

atoh ?

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
Ludde72
Asked:
Ludde72
  • 4
  • 3
  • 3
  • +1
1 Solution
 
imaki06Commented:
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
 
sjklein42Commented:
try scanf

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

Open in new window

0
 
ZoppoCommented:
@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
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
phoffricCommented:
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
 
phoffricCommented:
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
 
ZoppoCommented:
@phoffric: Thanks, I didn't remember this correctly - fine that you pointed this out ...
0
 
sjklein42Commented:
Zoppo,

sscanf is C++

scanf is C

0
 
phoffricCommented:
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
 
ZoppoCommented:
@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
 
sjklein42Commented:
Zoppo,

You're right.  I meant sscanf.

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

Apologies
0
 
ZoppoCommented:
No problem :o)
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now