Bitwise operation

Discussion in 'C Programming' started by Magix, Jul 30, 2004.

  1. Magix

    Magix Guest

    Hi,

    I want to manipulate an incoming WORD as I receive byte by byte. let say 07
    D4, which is 2004
    I want to combine these two bytes and convert into decimal, then convert as
    a string. (sprintf)

    int MSB;
    int LSB;
    int Total;
    char buf[5];

    MSB = (0x07 << 8)
    LSB= (0xD4 & 0x00FF)
    Total = MSB | LSB
    sprintf(buf, "%d",Total);

    When I print buf in %s format, the output I expected is 2004. But it prints
    "-long printf)d"
    What is wrong in my code above ?

    Thanks.
     
    Magix, Jul 30, 2004
    #1
    1. Advertising

  2. Magix

    Jack Klein Guest

    On Fri, 30 Jul 2004 10:11:00 +0800, "Magix" <> wrote in
    comp.lang.c:

    > Hi,
    >
    > I want to manipulate an incoming WORD as I receive byte by byte. let say 07
    > D4, which is 2004
    > I want to combine these two bytes and convert into decimal, then convert as
    > a string. (sprintf)
    >
    > int MSB;
    > int LSB;
    > int Total;
    > char buf[5];
    >
    > MSB = (0x07 << 8)
    > LSB= (0xD4 & 0x00FF)
    > Total = MSB | LSB
    > sprintf(buf, "%d",Total);
    >
    > When I print buf in %s format, the output I expected is 2004. But it prints
    > "-long printf)d"
    > What is wrong in my code above ?
    >
    > Thanks.


    There's nothing particularly wrong with what you posted. Copy the
    real code from your editor and paste the actual text into another
    post.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 30, 2004
    #2
    1. Advertising

  3. Magix wrote on 30/07/04 :
    > int MSB;
    > int LSB;
    > int Total;
    > char buf[5];
    >
    > MSB = (0x07 << 8)
    > LSB= (0xD4 & 0x00FF)
    > Total = MSB | LSB
    > sprintf(buf, "%d",Total);


    This code is not compiling. Please post the exact code that is not
    working.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Jul 30, 2004
    #3
  4. Magix

    cody Guest

    I should work this way but try %i instead of %d.

    --
    cody

    Freeware Tools, Games and Humour
    http://www.deutronium.de.vu || http://www.deutronium.tk
    "Magix" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > I want to manipulate an incoming WORD as I receive byte by byte. let say

    07
    > D4, which is 2004
    > I want to combine these two bytes and convert into decimal, then convert

    as
    > a string. (sprintf)
    >
    > int MSB;
    > int LSB;
    > int Total;
    > char buf[5];
    >
    > MSB = (0x07 << 8)
    > LSB= (0xD4 & 0x00FF)
    > Total = MSB | LSB
    > sprintf(buf, "%d",Total);
    >
    > When I print buf in %s format, the output I expected is 2004. But it

    prints
    > "-long printf)d"
    > What is wrong in my code above ?
    >
    > Thanks.
    >
    >
    >
     
    cody, Jul 30, 2004
    #4
  5. cody wrote on 30/07/04 :
    > I should work this way but try %i instead of %d.


    Why ? What do you think the difference is with printf()?

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Jul 30, 2004
    #5
  6. Magix

    Tim Hagan Guest

    "Magix" <> wrote in message news:...
    > Hi,
    >
    > I want to manipulate an incoming WORD as I receive byte by byte. let say 07
    > D4, which is 2004
    > I want to combine these two bytes and convert into decimal, then convert as
    > a string. (sprintf)
    >
    > int MSB;
    > int LSB;
    > int Total;
    > char buf[5];
    >
    > MSB = (0x07 << 8)
    > LSB= (0xD4 & 0x00FF)
    > Total = MSB | LSB
    > sprintf(buf, "%d",Total);
    >
    > When I print buf in %s format, the output I expected is 2004. But it prints
    > "-long printf)d"
    > What is wrong in my code above ?


    It doesn't compile. Besides, you omitted the most important piece of the
    puzzle: the offending printf statement. Try this:

    #include <stdio.h>

    int main(void)
    {
    int MSB;
    int LSB;
    int Total;
    char buf[5];

    MSB = (0x07 << 8);
    LSB= (0xD4 & 0x00FF);
    Total = MSB | LSB;
    sprintf(buf, "%d",Total);
    printf("%s\n", buf);
    return 0;
    }

    --
    Tim Hagan
     
    Tim Hagan, Jul 30, 2004
    #6
  7. Magix

    Joe Wright Guest

    Magix wrote:
    > Hi,
    >
    > I want to manipulate an incoming WORD as I receive byte by byte. let say 07
    > D4, which is 2004
    > I want to combine these two bytes and convert into decimal, then convert as
    > a string. (sprintf)
    >
    > int MSB;
    > int LSB;
    > int Total;
    > char buf[5];
    >
    > MSB = (0x07 << 8)
    > LSB= (0xD4 & 0x00FF)
    > Total = MSB | LSB
    > sprintf(buf, "%d",Total);
    >
    > When I print buf in %s format, the output I expected is 2004. But it prints
    > "-long printf)d"
    > What is wrong in my code above ?
    >
    > Thanks.
    >


    You're missing at least three semicolons.

    #include <stdio.h>
    int main(void)
    {
    int MSB;
    int LSB;
    int Total;
    char buf[5];
    MSB = (0x07 << 8);
    LSB = (0xD4 & 0x00FF);
    Total = MSB | LSB;
    sprintf(buf, "%d", Total);
    printf("%s\n", buf);
    return 0;
    }

    Mine works, why not yours? :)
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 31, 2004
    #7
    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. Pasquale Imbemba

    Bitwise Operation

    Pasquale Imbemba, May 6, 2004, in forum: Java
    Replies:
    2
    Views:
    554
    Roedy Green
    May 7, 2004
  2. biswaranjan.rath

    bitwise AND operation in xslt

    biswaranjan.rath, May 8, 2006, in forum: XML
    Replies:
    3
    Views:
    4,916
    shaunroe
    Nov 12, 2008
  3. Patrick Hoonhout

    bitwise operation...

    Patrick Hoonhout, Aug 27, 2003, in forum: C Programming
    Replies:
    13
    Views:
    673
    Alan Balmer
    Aug 28, 2003
  4. Magix

    Bitwise Operation

    Magix, Oct 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    561
    Dimension
    Oct 15, 2004
  5. Jerry

    Bitwise operation for division

    Jerry, Mar 2, 2005, in forum: C Programming
    Replies:
    7
    Views:
    8,262
    Chris Williams
    Mar 2, 2005
Loading...

Share This Page