Jump to content

String of Hex to Integer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
$AP

$AP

    Newbie

  • Members
  • PipPip
  • 24 posts
How can I convert a string consisting of hexadecimal digits into an integer number??

For example... if s[6]="A3B93D", then, how can I covert this string to an integer variable, say, n such that o/p of printf("%x",n) will be A3B93D ?

#2
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
You can use sscanf. :)

Edited by PythonPower, 09 February 2009 - 04:22 AM.


#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Assuming you're using a char array, you can run through the digits, with
sum=sum*16 + digit
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
$AP

$AP

    Newbie

  • Members
  • PipPip
  • 24 posts
the method : sum=s[0]*pow(16,5)+s[1]*pow(16,4)+ .....

or sum=(int)s[0]*0x100000+(int)s[1]*0x10000+....

none of the above is working.... nor the built-in functions like sscanf or atoi ...

Can anyone please give me the code snippet for this?

#5
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
Sorry, my first post was wrong. sscanf is what you need (I'll edit it)! ;)

Here's an example in C:


#include "stdlib.h"

#include "stdio.h"

#include "math.h"

#include "string.h"



int main(void)

{

	char *x = "A3B93D";

	int result = 0;

	

	sscanf(x, "%x", &result);

	

	printf("%s in hex is %i\n", x, result);

	

	return 0;

}




#6
$AP

$AP

    Newbie

  • Members
  • PipPip
  • 24 posts
Nope, its not showing properly. :confused:

@PythonPower, printf("%s in hex is %i\n", x, result); this statement will print A3B93D in hex is -18115. Thats what I am not wanting. I want A3B93D to be printed. Instead of %i, if I use %x it is always showing least 4 hex-digits,i.e. B93D.

Can anyone please modify it so that it give proper result??

#7
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
Well, firstly you can't store A3B93D as an int since it's too large (you'll have to use a long integer). Secondly, I'll post the code in a few hours. :thumbup1:

#8
$AP

$AP

    Newbie

  • Members
  • PipPip
  • 24 posts
Well, so far I am concerned, Hex Numbers are stored as a bit-pattern. So, we need to concentrate on that too.

I have another question. How can I check whether a integer n contains a decimal number or an hexadecimal number??

#9
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts

$AP said:

I have another question. How can I check whether a integer n contains a decimal number or an hexadecimal number??

You can't. Consider '123' is both a valid decimal and hexadecimal number.

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

$AP said:

I have another question. How can I check whether a integer n contains a decimal number or an hexadecimal number??

To take it further, not only can't you, but the question doesn't make sense. Decimal and Hexadecimal are representations of a number. Consider the number of fingers on your hand (including thumbs). The decimal representation for that number is 10, while the hexadecimal is A. If you consider fingers and toes, the representations are 20 and 14.

C and C++ store the quantity in an int and provide the desired representation based on your request. Internally, 10 is represented as 00001010, and 20 as 00010100... in binary.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
$AP

$AP

    Newbie

  • Members
  • PipPip
  • 24 posts
Yea, thats fine... But possibly hexadecimal numbers are stored in integer variable as a BIT PATTERN. If we initialize a variable with a decimal number, say 65, then we write int n=65; but if we want to initialize a integer variable with a hexadecimal number, say 8AB, then we write int n=0x8AB, where this A B are stored as BIT PATTERN in the 2 bytes allocated for an integer variable. Now, if an integer variable say n is passed to a function which may either contain either a decimal or a hexadecimal number, then how can I be able to understand which kind of value is passed, if possible...

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can't. 0x8AB is a value. ints do NOT store whether the last assignment was based on hex, oct, or decimal.

0x8AB is 2219 in decimal is 04253 (octal). They are all the same value with different representations. An int stores the value, not the representation.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog