How to combine two bytes in order to get the original value

firstoption

Member
Joined
Apr 23, 2014
Messages
10
Programming Experience
Beginner
Good day to all,
Please I need your help and support on how to combine two bytes (each byte is 8 bits wide) in order to get the original value. I am displaying values(from 0 to 500)sent from my microcontroller on the GUI.I can successfully display values from 0 to 255 as this requires just sending a byte. However sending values from 256 to 500 requires sending two bytes. The problem I am having is that I was unable to re-combine the received two bytes in order to get the original value. Below are my lines of code:
Code:
int  main( void ) // this is the main function inside the microcontroller
{
 sei();
 USI_TWI_Master_Initialise();
 Spi_Master_Init();
 
 
 while(1)  // I am using this loop to send 500(111110100)
 {
              
  
  Transmitt_Receive(244);//this function sends LOW BYTE of 500(11110100)
  _delay_ms(1000);
  Transmitt_Receive(1); // this function sends HIGH BYTE of 500(00000001)
  _delay_ms(1000);
  
  
  
 }
}
Code:
private   void rxtemp_CheckedChanged(object sender, EventArgs e)
       {
           UInt32 numBytesRead = 0;
           UInt32 numBytesToRead =1 ;
           Int32 value;
           byte[] readData = new byte[5];
           byte[] val      = new byte[5];
          
           Thread th = new Thread(() =>
               {
                   while (true)
                   {
                       ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
                       val[0]   = readData[0];
                       ftStatus = myFtdiDevice.Read(readData, numBytesToRead, ref numBytesRead);
                       val[1]   = readData[0];
               
                       value = (val[0] << 8) | val[1]; 
                           label3.Invoke(new Action(() =>
                           {
                               label3.Text = value + "?C";

                           }));

                         
                    
                   }
                   
                    
               });
           th.IsBackground = true;
           th.Start(); 
       }
When I combined the two bytes using the above arrangements, instead of getting 500 my GUI displayed 62708.I got the same result when I used BitConverter.
Code:
     value3 = BitConverter.ToInt32(val,0);
I would be very glad if somebody could put me through on how to resolve this error. Thank you for the support. Best regards.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
value = (val[0] << 8) | val[1]; 

This line shifts your least significant byte 8 positions (multiplying it with 256) and next adds the most significant byte. Hence the extreme result. You should shift the most significant byte and next add the least significant byte.

Your value3 gives the correct result.

Below my little test program (without modification)
        static void Main(string[] args)
        {
            byte lsb = 244;
            byte msb = 1;

            byte[] val = new byte[5];
            val[0] = lsb;
            val[1] = msb;

            int value = (val[0] << 8) | val[1];
            int value3 = BitConverter.ToInt32(val, 0);
            Console.WriteLine(value + ":" + value3);
            Console.ReadLine();
            return;
}

The result is
Code:
62465:500
 
Top Bottom