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 ?
String of Hex to Integer
Started by $AP, Feb 08 2009 10:23 AM
13 replies to this topic
#1
Posted 08 February 2009 - 10:23 AM
|
|
|
#2
Posted 08 February 2009 - 12:47 PM
#3
Posted 08 February 2009 - 04:17 PM
Assuming you're using a char array, you can run through the digits, with
sum=sum*16 + digit
sum=sum*16 + digit
#4
Posted 09 February 2009 - 12:09 AM
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?
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
Posted 09 February 2009 - 04:21 AM
Sorry, my first post was wrong. sscanf is what you need (I'll edit it)! ;)
Here's an example in C:
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
Posted 10 February 2009 - 08:32 AM
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??
@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
Posted 10 February 2009 - 08:37 AM
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
Posted 11 February 2009 - 09:41 AM
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??
I have another question. How can I check whether a integer n contains a decimal number or an hexadecimal number??
#9
Posted 11 February 2009 - 10:03 AM
$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
Posted 11 February 2009 - 10:16 AM
$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.
#11
Posted 12 February 2009 - 08:24 AM
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
Posted 12 February 2009 - 08:52 AM
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.
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.


Sign In
Create Account


Back to top









