473,324 Members | 2,541 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,324 software developers and data experts.

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 2535

"sara" <sa************@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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_string, "0x%x", your_value);
Nov 14 '05 #2

dandelion wrote:
"sara" <sa************@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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_string, "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.googlegr oups.com:

dandelion wrote:
"sara" <sa************@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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_string, "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.googlegr oups.com...

dandelion wrote:
"sara" <sa************@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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_string, "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*******@hotmail.com> schreef in bericht
news:Xn********************************@130.133.1. 4...
"sara" <sa************@rediffmail.com> wrote in
news:11**********************@z14g2000cwz.googlegr oups.com:

dandelion wrote:
"sara" <sa************@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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_string, "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.nl> wrote in news:10************@corp.supernews.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*******@hotmail.com> schreef in bericht
news:Xn********************************@130.133.1. 4...
"sara" <sa************@rediffmail.com> wrote in
news:11**********************@z14g2000cwz.googlegr oups.com:

dandelion wrote:
"sara" <sa************@rediffmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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_string, "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
Hi everybody,
I do accept all of your comments. Every data stored in the
memroy in the form of binary. Its upto the need to print the data in
hex, or octal or binary or as decimal format.
BTW what I actually want is that , suppose the lenght of
the
data is 1234 bytes to be transmitted, I need to write into some
registers the length of the data in the form of hex. for eg REG
ABC=0x0004D209. here 4D2 is the lenght of hte bytes in hex form that
has to written into the registers ABC leaving the first 2 bytes( 09
here) which are reserved.
so for writing this type of hex into reg I discussed the conversion.

Thanks
Sara

Mark A. Odell wrote:
"Johan" <me@knoware.nl> wrote in news:10************@corp.supernews.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*******@hotmail.com> schreef in bericht
news:Xn********************************@130.133.1. 4...
"sara" <sa************@rediffmail.com> wrote in
news:11**********************@z14g2000cwz.googlegr oups.com:
dandelion wrote:
> "sara" <sa************@rediffmail.com> wrote in message
> news:11**********************@f14g2000cwb.googlegr oups.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_string, "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 #11
sara wrote:
I do accept all of your comments. Every data stored in the
memroy in the form of binary. Its upto the need to print the data in
hex, or octal or binary or as decimal format.
BTW what I actually want is that , suppose the lenght of
the
data is 1234 bytes to be transmitted, I need to write into some
registers the length of the data in the form of hex. for eg REG
ABC=0x0004D209. here 4D2 is the lenght of hte bytes in hex form that
has to written into the registers ABC leaving the first 2 bytes( 09
here) which are reserved.
so for writing this type of hex into reg I discussed the conversion.


Contrary to what you wrote, you did not accept what you were told, since
you insist on discussing hexadecimal representations.

ABC = 1234<<8|9;

Note there is nothing hexadecimal.
(Also note that 09 in 0x0004D209 hardly constitutes 2 bytes.)

Dietmar
Nov 14 '05 #12

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')
4
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...
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...
14
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...
14
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: ...
6
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
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...
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)
26
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
23
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.