954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

creating byte stream

how do i assign -let's say number 12000- to 3-byte stream?
could u plz give a little code example?

thanx.

asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Hi
Did that mean you want to assign any number that can be stored in 3 bytes storage space? if so, I would suggest the following code. (I think if taken unsigned the maximum number would be 0xFFFFFF = 16777215 in decimal.) Providing a simple example. can not consider it perfect solution though.

unsigned char ch[3];
    unsigned long int d;
    scanf("%ld",&d);
    if(d<0xffffff)
    {
        ch[0]= d&0xFF;
        ch[1]= (d>>8)&0xFF;
        ch[2]= (d>>16)&0xFF;
    }
ashishtrivedi
Newbie Poster
12 posts since Jul 2005
Reputation Points: 13
Solved Threads: 2
 

Why do u want to place that in a 3 byte stream when that can fit in 2 byte stream.

ssharish2005

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 
Hi Did that mean you want to assign any number that can be stored in 3 bytes storage space?


Yes.

ch[0]= d&0xFF;
        ch[1]= (d>>8)&0xFF;



let's say d = 1200;
what does these lines do? could u show the results in bitstreams like 0010101.......?

asilter
Junior Poster in Training
60 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
/* This is the explanaton 
  ch[0] = d & 0xFF;       --->    0000 0100 1011 0000
                                & 0000 0000 1111 1111
                                  ------------------- 
                                  0000 0000 1011 0000   --> ch[0]
                                  -------------------
 
  ch[1] = (d >> 8) & 0xFF  -->    0000 0000 0000 0100
                                & 0000 0000 1111 1111   
                                  -------------------
                                  0000 0000 0000 0100   -->  ch[1]
                                  -------------------
 
*/


ssharish2005

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You