473,508 Members | 3,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conversion from Hexadecimal number into Decimal number

Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.

Nov 13 '06 #1
14 16199
dh*******@gmail.com wrote:
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.
u is still on holiday.

You show us yours and someone might show you theirs....

--
Ian Collins.
Nov 13 '06 #2
dh*******@gmail.com said:
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number.
You ask the impossible. There is no such thing as a hexadecimal number, and
no such thing as a decimal number. Hexadecimal and decimal are merely
representations. The number itself is independent of "base". A dozen apples
is a dozen apples, no matter how many fingers you have, and no matter
whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11, 10, or
C. All that matters is that writer and reader(s) both understand the
representation being used.

What you appear to be asking for is how to convert a given representation
into another representation. That's easy enough to do. Simply build an
integer n from whichever representation you have, and then do this to build
a representation in a given number base:

s = empty string
while n 0 do
digit = n mod base
add text representation of digit to the end of s
divide n by base, giving n
elihw
if s is empty
s = "0"
else
reverse s in place
fi

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 13 '06 #3
Richard Heathfield wrote:
dh*******@gmail.com said:
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number.

You ask the impossible. There is no such thing as a hexadecimal number, and
no such thing as a decimal number. Hexadecimal and decimal are merely
representations. The number itself is independent of "base". A dozen apples
is a dozen apples, no matter how many fingers you have, and no matter
whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11, 10, or
C. All that matters is that writer and reader(s) both understand the
representation being used.
I would argue that 'dozen' itself is another representation, albiet a
very arbitrary one. I've not studied mathematics beyond junior college,
but let me ask this: Can there be numbers apart from some form of
representation?

Nov 13 '06 #4
Hello,
I would argue that 'dozen' itself is another representation, albiet a
very arbitrary one. I've not studied mathematics beyond junior college,
but let me ask this: Can there be numbers apart from some form of
representation?
I don't want to step into abstract mathematical argumentations, but the
short answer to your question is: "yes". See:

http://en.wikipedia.org/wiki/Natural_number

Regards,
Loic.

Nov 13 '06 #5
santosh said:
Richard Heathfield wrote:
<snip>
>A dozen
apples is a dozen apples, no matter how many fingers you have, and no
matter whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11,
10, or C. All that matters is that writer and reader(s) both understand
the representation being used.

I would argue that 'dozen' itself is another representation, albiet a
very arbitrary one.
Indeed it is. It's "English representation", if you like. Discussing numbers
without representing them in *some* fashion is rather awkward, but consider
the following:

* * * * *
+ + + + +
<>< <>< <>< <>< <><

No matter how you represent the number of asterisks, the number of plus
signs, and the number of fish I just showed, it is nevertheless clear that
there is the same number of each. That sameness, that one-to-one
correspondence, that "matching", is what 'number' is all about.
I've not studied mathematics beyond junior college,
but let me ask this: Can there be numbers apart from some form of
representation?
Very much so, yes - but talking about them can get a bit abstract at times.
:-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 13 '06 #6
dh*******@gmail.com wrote:
# Hi friends,
# I need a sample code in C which will convert a Hexadecimal
# number into decimal number. I had written a code for that but it was
# too long, I need a small code, so request u all to provide me with
# small sample code for that.

strtol(string,0,16)

--
SM Ryan http://www.rawbw.com/~wyrmwif/
No pleasure, no rapture, no exquisite sin greater than central air.
Nov 13 '06 #7
:
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.

Your question is very vague and generic, so I'll give you code which is
rather generic.

A number will be represented by a sequence of digits. What's a digit? Is it
a character? Is it a floating-point number? Is it a pointer? I don't know,
so I'll write generic code.

How will we arrange these digits? Well let's decide that we'll have a null-
terminated array which starts with the Most Significant Digit.

In providing a function which converts from one representation to another,
we need also to receive a look-up table to map a digit value to an actual
digit (and vice-versa). This look-up may come in the form of a function, or
of an array. I think a function might be more convenient. Now, we can write
the function as follows:

(Unchecked, may contain a subtle bug or two.)

#include <assert.h>

void ConvertBetweenRadices(DigitType *const p_arr,
unsigned const from_radix,
unsigned (*const pFrom)(DigitType),
unsigned const to_radix,
DigitType (*const pTo)(unsigned))
{
assert(!!p_arr); assert(!!*p_arr); assert(!!from_radix);
assert(!!pFrom); assert(!!to_radix); assert(!!pTo);

{
long unsigned val = 0;
long unsigned multiplier = 1;

DigitType *p = p_arr;

val += pFrom(*p++);

while (*p) val += pFrom(*p++) * (multiplier*=from_radix);

p = p_arr;
do
{
*p++ = pTo(val % to_radix);
val /= to_radix;
} while (val);

*p = 0;
}}

As you can see, the function assumes the precondition that the buffer is
long enough. It may be used something like the following:

(Again, unchecked.)

#include <assert.h>

typedef char DigitType;

#include "cbr.h" /* Code shown above */

unsigned DecToVal(DigitType const x)
{
assert(x>='0' && x<='9');

return x - '0';
}

DigitType ValToHex(unsigned const x)
{
assert(x<=15);

if (x < 10) return '0' + x;

switch(x)
{
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
}
}

#include <stdio.h>

int main(void)
{
char buf[24] = "654323";

ConvertBetweenRadices(buf,10,DecToVal,16,ValToHex) ;

puts(buf);

return 0;
}

It was only after having written the code that I realised we* write from
left to right putting the Most Significant Digit first, rather than the
Least Significant Digit. Ah well.

*we : except for our Arabic particpants of course!

--

Frederick Gotham
Nov 13 '06 #8
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrote:
dh*******@gmail.com wrote:
# I need a sample code in C which will convert a Hexadecimal
# number into decimal number. I had written a code for that but it was
# too long, I need a small code, so request u all to provide me with
# small sample code for that.
# error This is not a quoting character suitable for Usenet.
strtol(string,0,16)
That's a function that will turn the hexadecimal text representation of
a number into an actual number, in binary. To turn _that_ into a decimal
number, you need another function which is equally easily found in any
handbook on C.

Richard
Nov 13 '06 #9
<dh*******@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.
Although they're a bit pricey, I would recommend you buy Knuth's classic
3-volume work "The Art of Computer Programming". Knuth covers all the basic
integer algorithms there, including radix conversion.

The other poster (who included the C-code) appears at first glance to have
simply provided you the standard algorithm. Very nice of him, but a waste
of his time.

Dave.

Nov 13 '06 #10
"santosh" <sa*********@gmail.comwrites:
Richard Heathfield wrote:
>dh*******@gmail.com said:
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number.

You ask the impossible. There is no such thing as a hexadecimal number, and
no such thing as a decimal number. Hexadecimal and decimal are merely
representations. The number itself is independent of "base". A dozen apples
is a dozen apples, no matter how many fingers you have, and no matter
whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11, 10, or
C. All that matters is that writer and reader(s) both understand the
representation being used.

I would argue that 'dozen' itself is another representation, albiet a
very arbitrary one. I've not studied mathematics beyond junior college,
but let me ask this: Can there be numbers apart from some form of
representation?

Tell me, how is "dozen" any more arbitrary than "twelve"? It isnt.

--
Nov 14 '06 #11

Ian Collins wrote:
You show us yours and someone might show you theirs....
Uh, isn't there an alt.sex.personals for
queries of this ilk?

James

Nov 14 '06 #12
On Nov 13, 1:18 am, dharmd...@gmail.com wrote:
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.
Please reply to the group as I don't check email often.

Just use strtoul() or the like. sscanf() is somewhat easier to use but
is less robust.

Nov 14 '06 #13
2006-11-14 <11*********************@i42g2000cwa.googlegroups. com>,
santosh wrote:
On Nov 13, 1:18 am, dharmd...@gmail.com wrote:
>Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.

Please reply to the group as I don't check email often.

Just use strtoul() or the like. sscanf() is somewhat easier to use but
is less robust.
sscanf() is only easier to use if you use it wrong.

I would bet, though, that he's got a homework assignment to do it
himself.

To the original poster: post your existing code and maybe people can
give you ideas on how to tighten it up.
Nov 14 '06 #14
dh*******@gmail.com wrote:
Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.
You won't get much smaller than this:

sprintf(d,"%ld",strtol(s,0,16));

It will read from the string s a number in hexadecimal form. It will
convert that number to a long int. It will write that number as a string
in decimal form into the memory starting at d, which must be long enough
to contain any required value.

--
Simon.
Nov 20 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11137
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
2
16024
by: Raja | last post by:
IS there any inbuilt decimal to binary conversion function in C++
3
17821
by: Amjad | last post by:
Hi, Are there any built-in methods in VB.NET that convert one number in one format to its equivalent in another format? For example I want to convert the number 162 (decimal) to 10100010...
15
35444
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
6
16883
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
14
3168
by: Ellipsis | last post by:
Ok so I am converting to hexadecimal from decimal and I can only cout the reverse order of the hexadecimal?! How could I reverse this so its the right order? Heres my code: #include <iostream>...
5
3191
by: Praveena P | last post by:
Hi folks, I am new to Python... so am not too sure about how the type conversion works. I have to read a file that contains hexadecimal data and use the data further to do some arithmetic...
6
3372
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
5
6366
by: raghavendrap | last post by:
$tmp = 0xc0211082; $tmp1 = 0xc0000082; Hello friends, I got doubt when i am converting negative hexadecimal to a decimal number.Please look at mentioned example below. ...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.