decimal to hexadecimal

S

sara

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
 
D

dandelion

sara said:
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);
 
S

sara

dandelion said:
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
 
M

Mark A. Odell

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.
 
D

dandelion

sara said:
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...
 
J

john_bode

"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.
 
M

Malcolm

sara said:
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;"
 
M

Mark McIntyre

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.
 
S

sara

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
He wants to know how to output 100 in hex ?

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

Oh, then what did he mean by:

See how top posting screws everything up?
 
D

Dietmar Schindler

sara said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top