How to get two bytes form a buffer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cylin

    #1

    How to get two bytes form a buffer?

    Dear all,

    From now on, I just can get a byte twice.
    Then decode this two bytes.

    for example,
    buffer is a unsigned char array.

    value=(*buffer) <<8;
    value+=*(buffer +1);

    Is there any fast way to do that?

    Regards,
    cylin.


  • Sebastian \Psy\ Messerschmidt

    #2
    Re: How to get two bytes form a buffer?


    "cylin" <cylin@avant.co m.tw> schrieb im Newsbeitrag
    news:brugos$72m ju$1@ID-154203.news.uni-berlin.de...[color=blue]
    > Dear all,
    >
    > From now on, I just can get a byte twice.
    > Then decode this two bytes.
    >
    > for example,
    > buffer is a unsigned char array.
    >
    > value=(*buffer) <<8;
    > value+=*(buffer +1);
    >[/color]
    thats how I did it.
    but alternatives could be:

    memcpy(&value,b uffer,2);

    or some fancy casting:

    value = *((unsigned int*)&(buffer[i]))
    respective; value=*(unsigne d int*)(buffer);


    but I'm not sure if uint has two bytes size, see it for yourself


    hth psy


    Comment

    • Sean Kenwrick

      #3
      Re: How to get two bytes form a buffer?


      "Sebastian "Psy" Messerschmidt"
      <sebastian.mess erschmidt@DELET ETHISstud.tu-ilmenau.de> wrote in message
      news:bruhlf$i9n $1@piggy.rz.tu-ilmenau.de...[color=blue]
      >
      > "cylin" <cylin@avant.co m.tw> schrieb im Newsbeitrag
      > news:brugos$72m ju$1@ID-154203.news.uni-berlin.de...[color=green]
      > > Dear all,
      > >
      > > From now on, I just can get a byte twice.
      > > Then decode this two bytes.
      > >
      > > for example,
      > > buffer is a unsigned char array.
      > >
      > > value=(*buffer) <<8;
      > > value+=*(buffer +1);
      > >[/color]
      > thats how I did it.
      > but alternatives could be:
      >
      > memcpy(&value,b uffer,2);
      >
      > or some fancy casting:
      >
      > value = *((unsigned int*)&(buffer[i]))
      > respective; value=*(unsigne d int*)(buffer);
      >
      >
      > but I'm not sure if uint has two bytes size, see it for yourself
      >
      >
      > hth psy
      >[/color]

      value=*(short *) buffer;

      Sean


      Comment

      • Thomas Matthews

        #4
        Re: How to get two bytes form a buffer?

        Sean Kenwrick wrote:[color=blue]
        > "Sebastian "Psy" Messerschmidt"
        > <sebastian.mess erschmidt@DELET ETHISstud.tu-ilmenau.de> wrote in message
        > news:bruhlf$i9n $1@piggy.rz.tu-ilmenau.de...
        >[color=green]
        >>"cylin" <cylin@avant.co m.tw> schrieb im Newsbeitrag
        >>news:brugos$7 2mju$1@ID-154203.news.uni-berlin.de...
        >>[color=darkred]
        >>>Dear all,
        >>>
        >>>From now on, I just can get a byte twice.
        >>>Then decode this two bytes.
        >>>
        >>>for example,
        >>>buffer is a unsigned char array.
        >>>
        >>>value=(*buff er)<<8;
        >>>value+=*(buf fer+1);
        >>>[/color]
        >>
        >>thats how I did it.
        >>but alternatives could be:
        >>
        >>memcpy(&value ,buffer,2);
        >>
        >>or some fancy casting:
        >>
        >>value = *((unsigned int*)&(buffer[i]))
        >>respective; value=*(unsigne d int*)(buffer);
        >>
        >>
        >>but I'm not sure if uint has two bytes size, see it for yourself
        >>
        >>
        >>hth psy
        >>[/color]
        >
        >[/color]
        if (sizeof(short) == 2)
        {[color=blue]
        > value=*(short *) buffer;[/color]
        }
        [color=blue]
        >
        > Sean
        >
        >[/color]


        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Jack Klein

          #5
          Re: How to get two bytes form a buffer?

          On Fri, 19 Dec 2003 17:06:00 GMT, Thomas Matthews
          <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > wrote in comp.lang.c++:
          [color=blue]
          > Sean Kenwrick wrote:[color=green]
          > > "Sebastian "Psy" Messerschmidt"
          > > <sebastian.mess erschmidt@DELET ETHISstud.tu-ilmenau.de> wrote in message
          > > news:bruhlf$i9n $1@piggy.rz.tu-ilmenau.de...
          > >[color=darkred]
          > >>"cylin" <cylin@avant.co m.tw> schrieb im Newsbeitrag
          > >>news:brugos$7 2mju$1@ID-154203.news.uni-berlin.de...
          > >>
          > >>>Dear all,
          > >>>
          > >>>From now on, I just can get a byte twice.
          > >>>Then decode this two bytes.
          > >>>
          > >>>for example,
          > >>>buffer is a unsigned char array.
          > >>>
          > >>>value=(*buff er)<<8;
          > >>>value+=*(buf fer+1);
          > >>>
          > >>
          > >>thats how I did it.
          > >>but alternatives could be:
          > >>
          > >>memcpy(&value ,buffer,2);
          > >>
          > >>or some fancy casting:
          > >>
          > >>value = *((unsigned int*)&(buffer[i]))
          > >>respective; value=*(unsigne d int*)(buffer);
          > >>
          > >>
          > >>but I'm not sure if uint has two bytes size, see it for yourself
          > >>
          > >>
          > >>hth psy
          > >>[/color]
          > >
          > >[/color]
          > if (sizeof(short) == 2)[/color]

          ....and it the short is big-endian.
          [color=blue]
          > {[color=green]
          > > value=*(short *) buffer;[/color]
          > }[/color]

          --
          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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

          Comment

          • cylin

            #6
            Re: How to get two bytes form a buffer?

            But my buffer is 4k bytes length,
            and there are lot of such values in this buffer.
            It's not only in *(buffer) and *(buffer+1) but also everywhere.

            memcpy only can handle the first n bytes in the buffer.

            Thanks all to reply.

            Regards,
            cylin.

            "Jack Klein" <jackklein@spam cop.net>
            ???????:tkm7uvo 2d88ampri3734oh t81tqibgn9ne@4a x.com...
            On Fri, 19 Dec 2003 17:06:00 GMT, Thomas Matthews
            <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > wrote in comp.lang.c++:
            [color=blue]
            > Sean Kenwrick wrote:[color=green]
            > > "Sebastian "Psy" Messerschmidt"
            > > <sebastian.mess erschmidt@DELET ETHISstud.tu-ilmenau.de> wrote in message
            > > news:bruhlf$i9n $1@piggy.rz.tu-ilmenau.de...
            > >[color=darkred]
            > >>"cylin" <cylin@avant.co m.tw> schrieb im Newsbeitrag
            > >>news:brugos$7 2mju$1@ID-154203.news.uni-berlin.de...
            > >>
            > >>>Dear all,
            > >>>
            > >>>From now on, I just can get a byte twice.
            > >>>Then decode this two bytes.
            > >>>
            > >>>for example,
            > >>>buffer is a unsigned char array.
            > >>>
            > >>>value=(*buff er)<<8;
            > >>>value+=*(buf fer+1);
            > >>>
            > >>
            > >>thats how I did it.
            > >>but alternatives could be:
            > >>
            > >>memcpy(&value ,buffer,2);
            > >>
            > >>or some fancy casting:
            > >>
            > >>value = *((unsigned int*)&(buffer[i]))
            > >>respective; value=*(unsigne d int*)(buffer);
            > >>
            > >>
            > >>but I'm not sure if uint has two bytes size, see it for yourself
            > >>
            > >>
            > >>hth psy
            > >>[/color]
            > >
            > >[/color]
            > if (sizeof(short) == 2)[/color]

            ....and it the short is big-endian.
            [color=blue]
            > {[color=green]
            > > value=*(short *) buffer;[/color]
            > }[/color]

            --
            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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq


            Comment

            Working...