decimal to hexadecimal

Discussion in 'C Programming' started by sara, Dec 28, 2004.

  1. sara

    sara Guest

    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
     
    sara, Dec 28, 2004
    #1
    1. Advertising

  2. sara

    dandelion Guest

    "sara" <> wrote in message
    news:...
    > 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);
     
    dandelion, Dec 28, 2004
    #2
    1. Advertising

  3. sara

    sara Guest

    dandelion wrote:
    > "sara" <> wrote in message
    > news:...
    > > 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
     
    sara, Dec 28, 2004
    #3
  4. "sara" <> wrote in
    news::

    >
    > dandelion wrote:
    >> "sara" <> wrote in message
    >> news:...
    >> > 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, Dec 28, 2004
    #4
  5. sara

    dandelion Guest

    "sara" <> wrote in message
    news:...
    >
    > dandelion wrote:
    > > "sara" <> wrote in message
    > > news:...
    > > > 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...
     
    dandelion, Dec 28, 2004
    #5
  6. sara

    Guest

    "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.
     
    , Dec 28, 2004
    #6
  7. sara

    Malcolm Guest

    "sara" <> 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;"
     
    Malcolm, Dec 28, 2004
    #7
  8. On 28 Dec 2004 05:57:01 -0800, in comp.lang.c , "sara"
    <> 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 =----
     
    Mark McIntyre, Dec 29, 2004
    #8
  9. sara

    Johan Guest

    He wants to know how to output 100 in hex ?

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

    John

    "Mark A. Odell" <> schreef in bericht
    news:Xns95CD5C86FC0D3CopyrightMarkOdell@130.133.1.4...
    > "sara" <> wrote in
    > news::
    >
    >>
    >> dandelion wrote:
    >>> "sara" <> wrote in message
    >>> news:...
    >>> > 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 ->
    > --
     
    Johan, Dec 29, 2004
    #9
  10. "Johan" <> wrote in news::

    > 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" <> schreef in bericht
    > news:Xns95CD5C86FC0D3CopyrightMarkOdell@130.133.1.4...
    >> "sara" <> wrote in
    >> news::
    >>
    >>>
    >>> dandelion wrote:
    >>>> "sara" <> wrote in message
    >>>> news:...
    >>>> > 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 ->
    --
     
    Mark A. Odell, Dec 29, 2004
    #10
  11. sara

    sara Guest

    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" <> wrote in

    news::
    >
    > > 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" <> schreef in bericht
    > > news:Xns95CD5C86FC0D3CopyrightMarkOdell@130.133.1.4...
    > >> "sara" <> wrote in
    > >> news::
    > >>
    > >>>
    > >>> dandelion wrote:
    > >>>> "sara" <> wrote in message
    > >>>> news:...
    > >>>> > 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 ->
    > --
     
    sara, Jan 4, 2005
    #11
  12. 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
     
    Dietmar Schindler, Jan 5, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. muss
    Replies:
    13
    Views:
    729
    Vladimir S. Oka
    Mar 27, 2006
  2. Replies:
    14
    Views:
    1,069
    Simon Biber
    Nov 16, 2006
  3. Replies:
    2
    Views:
    860
  4. Replies:
    6
    Views:
    643
    Keith Thompson
    Sep 2, 2007
  5. Replies:
    6
    Views:
    521
    Barry Schwarz
    Sep 4, 2007
Loading...

Share This Page