Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   decimal to hexadecimal (http://www.velocityreviews.com/forums/t436303-decimal-to-hexadecimal.html)

sara 12-28-2004 10:16 AM

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


dandelion 12-28-2004 10:39 AM

Re: decimal to hexadecimal
 

"sara" <saravanan_sprt@rediffmail.com> wrote in message
news:1104228970.687080.293440@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);



sara 12-28-2004 01:57 PM

Re: decimal to hexadecimal
 

dandelion wrote:
> "sara" <saravanan_sprt@rediffmail.com> wrote in message
> news:1104228970.687080.293440@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


Mark A. Odell 12-28-2004 02:05 PM

Re: decimal to hexadecimal
 
"sara" <saravanan_sprt@rediffmail.com> wrote in
news:1104242221.321398.116650@z14g2000cwz.googlegr oups.com:

>
> dandelion wrote:
>> "sara" <saravanan_sprt@rediffmail.com> wrote in message
>> news:1104228970.687080.293440@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 ->
--

dandelion 12-28-2004 02:11 PM

Re: decimal to hexadecimal
 

"sara" <saravanan_sprt@rediffmail.com> wrote in message
news:1104242221.321398.116650@z14g2000cwz.googlegr oups.com...
>
> dandelion wrote:
> > "sara" <saravanan_sprt@rediffmail.com> wrote in message
> > news:1104228970.687080.293440@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...



john_bode@my-deja.com 12-28-2004 02:55 PM

Re: decimal to hexadecimal
 
"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.


Malcolm 12-28-2004 08:10 PM

Re: decimal to hexadecimal
 
"sara" <saravanan_sprt@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;"



Mark McIntyre 12-29-2004 12:41 AM

Re: decimal to hexadecimal
 
On 28 Dec 2004 05:57:01 -0800, in comp.lang.c , "sara"
<saravanan_sprt@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 =----

Johan 12-29-2004 05:50 AM

Re: decimal to hexadecimal
 
He wants to know how to output 100 in hex ?

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

John

"Mark A. Odell" <odellmark@hotmail.com> schreef in bericht
news:Xns95CD5C86FC0D3CopyrightMarkOdell@130.133.1. 4...
> "sara" <saravanan_sprt@rediffmail.com> wrote in
> news:1104242221.321398.116650@z14g2000cwz.googlegr oups.com:
>
>>
>> dandelion wrote:
>>> "sara" <saravanan_sprt@rediffmail.com> wrote in message
>>> news:1104228970.687080.293440@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 A. Odell 12-29-2004 01:27 PM

Re: decimal to hexadecimal
 
"Johan" <me@knoware.nl> wrote in news:10t4hcmm5bdjcf@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" <odellmark@hotmail.com> schreef in bericht
> news:Xns95CD5C86FC0D3CopyrightMarkOdell@130.133.1. 4...
>> "sara" <saravanan_sprt@rediffmail.com> wrote in
>> news:1104242221.321398.116650@z14g2000cwz.googlegr oups.com:
>>
>>>
>>> dandelion wrote:
>>>> "sara" <saravanan_sprt@rediffmail.com> wrote in message
>>>> news:1104228970.687080.293440@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 ->
--


All times are GMT. The time now is 05:24 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.