473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

convert decimal to hexadecimal number

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.

Sep 2 '07 #1
6 16860
sw****************@yahoo.com said:
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.
Numbers don't have bases. Number /representations/ have bases.

It sounds to me like you wish to accept a textual representation in base
ten of an integer, and present that integer's value using a base
sixteen representation. Look up fgets (to capture the data as a
string), strtoul (to convert it to an unsigned long integer), and
printf (to display it in a base sixteen representation). You will find
the %lX format specifier helpful.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 2 '07 #2
sw****************@yahoo.com wrote:
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.
If your number is already in a numeric variable, just use sprintf or printf
or fprintf to convert to hexadecimal representation. The relevant format
specifier is lx. For example.

unsigned int num = 10;
printf("%lx\n", num);

If your number is still in a textual form, then you can use sscanf or
strtoul to convert it into a numeric value and then use the method above to
print it out in hexadecimal.

Sep 2 '07 #3
On Sep 2, 5:12 pm, Richard Heathfield <r...@see.sig.invalidwrote:
sweeet_addictio...@yahoo.com said:
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.

Numbers don't have bases. Number /representations/ have bases.

It sounds to me like you wish to accept a textual representation in base
ten of an integer, and present that integer's value using a base
sixteen representation. Look up fgets (to capture the data as a
string), strtoul (to convert it to an unsigned long integer), and
printf (to display it in a base sixteen representation). You will find
the %lX format specifier helpful.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Im sry may be i didnot frame the question properly..i need to accept
an integer value(decimal) and then after converting it into
hexadecimal value i need to write it into a file.i do not need to
print it..so using fprintf along with %lx would not help me.for eg..if
i have a decimal value of 60 to be passed to a function ..i need that
function to convert it into hexadecimal value(eg 3c) and then write it
into a file

Sep 2 '07 #4
On Sep 2, 6:32 pm, santosh <santosh....@gmail.comwrote:
sweeet_addictio...@yahoo.com wrote:
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.

If your number is already in a numeric variable, just use sprintf or printf
or fprintf to convert to hexadecimal representation. The relevant format
specifier is lx. For example.

unsigned int num = 10;
printf("%lx\n", num);

If your number is still in a textual form, then you can use sscanf or
strtoul to convert it into a numeric value and then use the method above to
print it out in hexadecimal.
Im sry may be i didnot frame the question properly..i need to accept
an integer value(decimal) and then after converting it into
hexadecimal value i need to write it into a file.i do not need to
print it..so using fprintf along with %lx would not help me.for eg..if
i have a decimal value of 60 to be passed to a function ..i need that
function to convert it into hexadecimal value(eg 3c) and then write it
into a file

Sep 2 '07 #5
sw****************@yahoo.com wrote:
On Sep 2, 5:12 pm, Richard Heathfield <r...@see.sig.invalidwrote:
> sweeet_addictio...@yahoo.com said:
>>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.
Numbers don't have bases. Number /representations/ have bases.

It sounds to me like you wish to accept a textual representation in base
ten of an integer, and present that integer's value using a base
sixteen representation. Look up fgets (to capture the data as a
string), strtoul (to convert it to an unsigned long integer), and
printf (to display it in a base sixteen representation). You will find
the %lX format specifier helpful.

Im sry may be i didnot frame the question properly..i need to accept
an integer value(decimal) and then after converting it into
hexadecimal value i need to write it into a file.
When Richard said "numbers don't have bases", it means that there is no
such thing as a hexadecimal value.
i do not need to
print it..so using fprintf along with %lx would not help me.for eg..if
i have a decimal value of 60 to be passed to a function ..i need that
function to convert it into hexadecimal value(eg 3c) and then write it
into a file
Formatting a number in hexadecimal and writing to a file is what fprintf
will do, using a %lx format specifier.

--
Thad
Sep 2 '07 #6
sw****************@yahoo.com writes:
[...]

Please don't quote signatures (the part of the article following the
"-- " delimiter).
Im sry may be i didnot frame the question properly..i need to accept
an integer value(decimal) and then after converting it into
hexadecimal value i need to write it into a file.i do not need to
print it..so using fprintf along with %lx would not help me.for eg..if
i have a decimal value of 60 to be passed to a function ..i need that
function to convert it into hexadecimal value(eg 3c) and then write it
into a file
When you say you want to write the hexadecimal value 3c to a file, do
you mean that you want the file to contain the characters '3' and 'c',
or do you want the actual raw non-textual value written to a file?

The latter is referred to as "binary", not hexadecimal. The term
hexadecimal refers only to a textual representation that uses the
digits '0'..'9' and the letters 'a'..'f' (or 'A'..'F'). (Binary data
is often displayed in hexadecimal, which leads some people to think
that it *is* hexadecimal, but it isn't; the process of displaying it
requires a conversion from one form to another.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 2 '07 #7

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

Similar topics

6
by: serpent17 | last post by:
Hello, I was looking at this: http://docs.python.org/lib/module-struct.html and tried the following >>> import struct >>> struct.calcsize('h') 2 >>> struct.calcsize('b')
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
9
by: FalkoG | last post by:
Hello colleague I want to convert a floating number for example 5236.9856982 to a hexadecimal number. I'd tried several things but my problem is to convert the position after decimal point. I...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
13
by: muss | last post by:
how to convert decimal number in a hexadecimal number using C? Please help me. Thanks.
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
5
by: sweeet_addiction16 | last post by:
im coding in c....i need to accept an integer value(decimal) and then after converting it into hexadecimal value i need to write it into a file.i do not need to print it..so using fprintf along...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
8
by: sivadhanekula | last post by:
Hi everyone... I have a data with both decimal and Hexadecimal numbers. I am extracting the required data from for analysis with the help of MINITAB 15. The problem is MINITAB 15 is not accepting...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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...

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.