473,651 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

decimal to hexadecimal

Hi everybody,
can anyone tell me how to convert decimal number into
hexadecimal of the folloxing format.

If I give deciaml value of num=100, I need the output in the hexa
decimal form as 0x64.

I can able to get teh hex value 64 which of type char. But actually I
want the ouput in the format of 0x64.
can anyone tell me how to handle this conversion?

Thanks

sara

Nov 14 '05 #1
11 2561

"sara" <sa************ @rediffmail.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I can able to get teh hex value 64 which of type char. But actually I
want the ouput in the format of 0x64.
can anyone tell me how to handle this conversion?


sprintf(your_st ring, "0x%x", your_value);
Nov 14 '05 #2

dandelion wrote:
"sara" <sa************ @rediffmail.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I can able to get teh hex value 64 which of type char. But actually I want the ouput in the format of 0x64.
can anyone tell me how to handle this conversion?


sprintf(your_st ring, "0x%x", your_value);


Hi dandelion,
Printf stmt can print in the desired format as what you said.
But I want it to store in a vaiable.
I did it simply by type casting.

a =100; /* need to convert it to hexa*/

long b=0x0 | a;
its ok for me . now .

Any how thanks dandelion.

Sara

Nov 14 '05 #3
"sara" <sa************ @rediffmail.com > wrote in
news:11******** **************@ z14g2000cwz.goo glegroups.com:

dandelion wrote:
"sara" <sa************ @rediffmail.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
> I can able to get teh hex value 64 which of type char. But actually I > want the ouput in the format of 0x64.
> can anyone tell me how to handle this conversion?
sprintf(your_st ring, "0x%x", your_value);


Hi dandelion,
Printf stmt can print in the desired format as what you said.
But I want it to store in a vaiable.
I did it simply by type casting.


You are confused, I think.
a =100; /* need to convert it to hexa*/
This sets 'a' to 100 decimal, 0x64, 0144 (octal), and 1100100 (binary) all
at the same time. The value is the value is the value. How you display it
is up to you.
long b=0x0 | a;
This effectively does nothing. The compiler might optimize this away or
produce assembly like this:

## Get 'a' into register 'r3'
li r3,a@l
lwz r3,a@ha(r3)

## Bit-OR zero (r0) with 'a', useless operation.
or r3,r0,r3

## Store result into 'b'.
li r4,b@l
stw r3,b@ha(r4)
its ok for me . now . f


It's not what you think then.

--
- Mark ->
--
Nov 14 '05 #4

"sara" <sa************ @rediffmail.com > wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .

dandelion wrote:
"sara" <sa************ @rediffmail.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I can able to get teh hex value 64 which of type char. But actually I want the ouput in the format of 0x64.
can anyone tell me how to handle this conversion?
sprintf(your_st ring, "0x%x", your_value);


Hi dandelion,
Printf stmt can print in the desired format as what you said.


sprintf uses a string (char foobar[]). It does not print anything.
But I want it to store in a vaiable.
I did it simply by type casting.

a =100; /* need to convert it to hexa*/

long b=0x0 | a;
Which will give you b == 100. A nice NOP.
its ok for me . now .
If that is what you want, juo might as just write

long b = 100;

Which will produce the same result.
Any how thanks dandelion.


My pleasure...
Nov 14 '05 #5
"Format" (binary, octal, decimal, hex) only comes into play when
displaying the value on some output device; when you said you "want
the output in the form of 0x64", dandelion assumed you meant output for
display. The internal representation of a value is binary, period.
For example, the following statements are all equivalent:

a = 100;
b = 0x64;
c = 0144;

All store the same value (decimal 100, binary 1100100) to the
respective variables.

Nov 14 '05 #6
"sara" <sa************ @rediffmail.com > wrote

a =100; /* need to convert it to hexa*/

long b=0x0 | a;
its ok for me . now .

You are a bit confused.

Internally computers always represent numbers in binary. It is not possible
for a human to view this representation directly, because it is electronic
and we cannot see electrons.

So to convert a number to human representation, you have several choices.
The obvious one is to convert the electronic representation into a pattern
of 1s and 0s which you display on a video screen. Thus when we talk about
"binary representation" we could mean one of three things, the pattern of
electrical charges that the computer uses internally, the pattern of glowing
dots that the user sees on the screen forming 1s and 0s, or the intermediate
format that the computer uses to go from electrical charges to glowing dots,
normally a representation of the number in ASCII.

Now binary numbers are quite difficult for the human eye to read, so usually
instead of outputing 1s and 0s, we output hexadecimal codes.

Since John Sacroboso introduced the Arabic number system, we in the West
have used a decimal system for representing numbers. C still uses this
convention, so

a = 100; in C means "put the value of a hundred (ten times ten) in variable
a".

However because programmers often like to know the binary bit pattern of the
numbers they use

a = 0xDEADBEEF; means "put the value 3735928559 in variable a"

the "Ox" tells the compiler that we are using the convention base 16 rather
than the convention base 10. However a = 0x10 and a = 16 means exactly the
same thing.

So your second line doesn't make any sort of sense. You are saying "put ten
time ten in variable a", then you are saying "do a logical or with zero, and
put the result in variable b".
You might as well just say "b = a;"
Nov 14 '05 #7
On 28 Dec 2004 05:57:01 -0800, in comp.lang.c , "sara"
<sa************ @rediffmail.com > wrote:
a =100; /* need to convert it to hexa*/
Sara, numbers don't have a format till you print them. They're stored in
binary. The above value 100 is stored in a binary representation in a. When
you printf it, you can tell it to print in say hex, or decimal, or octal or
whatever.
long b=0x0 | a;
its ok for me . now .


Its still a binary number, Your operation did nothing at all.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #8
He wants to know how to output 100 in hex ?

i.e. printf("%02x", your_value);

John

"Mark A. Odell" <od*******@hotm ail.com> schreef in bericht
news:Xn******** *************** *********@130.1 33.1.4...
"sara" <sa************ @rediffmail.com > wrote in
news:11******** **************@ z14g2000cwz.goo glegroups.com:

dandelion wrote:
"sara" <sa************ @rediffmail.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
> I can able to get teh hex value 64 which of type char. But actually

I
> want the ouput in the format of 0x64.
> can anyone tell me how to handle this conversion?

sprintf(your_st ring, "0x%x", your_value);


Hi dandelion,
Printf stmt can print in the desired format as what you said.
But I want it to store in a vaiable.
I did it simply by type casting.


You are confused, I think.
a =100; /* need to convert it to hexa*/


This sets 'a' to 100 decimal, 0x64, 0144 (octal), and 1100100 (binary) all
at the same time. The value is the value is the value. How you display it
is up to you.
long b=0x0 | a;


This effectively does nothing. The compiler might optimize this away or
produce assembly like this:

## Get 'a' into register 'r3'
li r3,a@l
lwz r3,a@ha(r3)

## Bit-OR zero (r0) with 'a', useless operation.
or r3,r0,r3

## Store result into 'b'.
li r4,b@l
stw r3,b@ha(r4)
its ok for me . now . f


It's not what you think then.

--
- Mark ->
--

Nov 14 '05 #9
"Johan" <me@knoware.n l> wrote in news:10******** ****@corp.super news.com:
He wants to know how to output 100 in hex ?

i.e. printf("%02x", your_value);
Oh, then what did he mean by:
But I want it to store in a vaiable.


See how top posting screws everything up?
John

"Mark A. Odell" <od*******@hotm ail.com> schreef in bericht
news:Xn******** *************** *********@130.1 33.1.4...
"sara" <sa************ @rediffmail.com > wrote in
news:11******** **************@ z14g2000cwz.goo glegroups.com:

dandelion wrote:
"sara" <sa************ @rediffmail.com > wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
> I can able to get teh hex value 64 which of type char. But actually
I
> want the ouput in the format of 0x64.
> can anyone tell me how to handle this conversion?

sprintf(your_st ring, "0x%x", your_value);

Hi dandelion,
Printf stmt can print in the desired format as what you said.
But I want it to store in a vaiable.
I did it simply by type casting.


You are confused, I think.
a =100; /* need to convert it to hexa*/


This sets 'a' to 100 decimal, 0x64, 0144 (octal), and 1100100 (binary)
all at the same time. The value is the value is the value. How you
display it is up to you.
long b=0x0 | a;


This effectively does nothing. The compiler might optimize this away or
produce assembly like this:

## Get 'a' into register 'r3'
li r3,a@l
lwz r3,a@ha(r3)

## Bit-OR zero (r0) with 'a', useless operation.
or r3,r0,r3

## Store result into 'b'.
li r4,b@l
stw r3,b@ha(r4)
its ok for me . now . f


It's not what you think then.

--
- Mark ->
--



--
- Mark ->
--
Nov 14 '05 #10

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

Similar topics

6
2636
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')
4
2955
by: Yodai | last post by:
Hi all.. I'm trying to program an application for an emmbedded sistem where an external processor introduces data into my RAM. The thing is some if the data I have to pick up is written in hexadecimal format, but some other is written on the memory positions in decimal format. How can I retrieve this information reading it as decimal? So far I use this:
2
3462
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
14
16220
by: dharmdeep | last post by:
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.
14
14248
by: me2 | last post by:
I am writing a little base conversion utility called base.c. This is what base does. $ base -127 Signed decimal: -127 Unsigned decimal: 4294967169 Hexidecimal: 0xffffff81 Octal: O37777777601 Binary: 1098 7654 3210 9876 5432 1098 7654 3210
6
16897
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.
3
8839
by: zgfareed | last post by:
My program converts decimal numbers from to binary and hexadecimal. I am having trouble with my output which is supposed to be in a certain format. Binary needs to be in the format of XXXX XXXX for numbers 0 to 255. What am doing wrong or not doing with my output? Source code: // code #include <iostream>
10
4946
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)
26
3690
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
23
9770
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
0
8349
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8795
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8576
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.