By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

How to get two bytes form a buffer?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"cylin" <cy***@avant.com.tw> schrieb im Newsbeitrag
news:br************@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=(*buffer)<<8;
value+=*(buffer+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=*(unsigned int*)(buffer);
but I'm not sure if uint has two bytes size, see it for yourself
hth psy
Jul 22 '05 #2

P: n/a

"Sebastian "Psy" Messerschmidt"
<se*********************@DELETETHISstud.tu-ilmenau.de> wrote in message
news:br**********@piggy.rz.tu-ilmenau.de...

"cylin" <cy***@avant.com.tw> schrieb im Newsbeitrag
news:br************@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=(*buffer)<<8;
value+=*(buffer+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=*(unsigned int*)(buffer);
but I'm not sure if uint has two bytes size, see it for yourself
hth psy


value=*(short *) buffer;

Sean
Jul 22 '05 #3

P: n/a
Sean Kenwrick wrote:
"Sebastian "Psy" Messerschmidt"
<se*********************@DELETETHISstud.tu-ilmenau.de> wrote in message
news:br**********@piggy.rz.tu-ilmenau.de...
"cylin" <cy***@avant.com.tw> schrieb im Newsbeitrag
news:br************@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=(*buffer)<<8;
value+=*(buffer+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=*(unsigned int*)(buffer);
but I'm not sure if uint has two bytes size, see it for yourself
hth psy


if (sizeof(short) == 2)
{ value=*(short *) buffer; }

Sean

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4

P: n/a
On Fri, 19 Dec 2003 17:06:00 GMT, Thomas Matthews
<Th****************************@sbcglobal.net> wrote in comp.lang.c++:
Sean Kenwrick wrote:
"Sebastian "Psy" Messerschmidt"
<se*********************@DELETETHISstud.tu-ilmenau.de> wrote in message
news:br**********@piggy.rz.tu-ilmenau.de...
"cylin" <cy***@avant.com.tw> schrieb im Newsbeitrag
news:br************@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=(*buffer)<<8;
value+=*(buffer+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=*(unsigned int*)(buffer);
but I'm not sure if uint has two bytes size, see it for yourself
hth psy

if (sizeof(short) == 2)


....and it the short is big-endian.
{
value=*(short *) buffer;

}


--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #5

P: n/a
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" <ja*******@spamcop.net>
???????:tk********************************@4ax.com ...
On Fri, 19 Dec 2003 17:06:00 GMT, Thomas Matthews
<Th****************************@sbcglobal.net> wrote in comp.lang.c++:
Sean Kenwrick wrote:
"Sebastian "Psy" Messerschmidt"
<se*********************@DELETETHISstud.tu-ilmenau.de> wrote in message
news:br**********@piggy.rz.tu-ilmenau.de...
"cylin" <cy***@avant.com.tw> schrieb im Newsbeitrag
news:br************@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=(*buffer)<<8;
value+=*(buffer+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=*(unsigned int*)(buffer);
but I'm not sure if uint has two bytes size, see it for yourself
hth psy

if (sizeof(short) == 2)


....and it the short is big-endian.
{
value=*(short *) buffer;

}


--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.