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

thanx.

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

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

ssharish2005

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.