472,960 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 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 16800
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.