How to get two bytes form a buffer?

C

cylin

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.
 
S

Sebastian \Psy\ Messerschmidt

cylin said:
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))
respective; value=*(unsigned int*)(buffer);


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


hth psy
 
S

Sean Kenwrick

"Sebastian "Psy" Messerschmidt"
cylin said:
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))
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
 
T

Thomas Matthews

Sean said:
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))
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
 
J

Jack Klein

Sean said:
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))
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.

--
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
 
C

cylin

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" <[email protected]>
???????:[email protected]...
Sean said:
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))
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.

--
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top