none
Reading Byte data and returning desired values RRS feed

  • Question

  • I am trying to read 2 bytes from a data stream, FD 7F.

    FD is in location 0

    7F is in location 1

    I need to read these values as a combination starting at location 1 then 0 = 7FFD

    convert this to an int and display in a label.

    also.

    read 2 bytes from a different stream, FF 7F

    FF is in location 4

    7F is in location 5

    I need to read these values as a combination starting at location 5 then 4 = 7FFF

    7FFF convert to DEC 32767

    required value = -1 + (32767 multiplied by 6.103515625e-5)

    Round up to 1 decimal place and output to a label.

    Any Help would be greatly appreciated.


    Alert for thread

    Wednesday, February 27, 2019 10:49 AM

All replies

  • If you already know how to read bytes from streams and display numbers in labels, then check this example:

    byte byte0 = 0xFD;
    byte byte1 = 0x7F;
    
    byte[] bytes = { byte0, byte1 };
    
    Int16 value1 = BitConverter.ToInt16( bytes, 0 );
    
    byte byte4 = 0xFF;
    byte byte5 = 0x7F;
    
    bytes = new[] { byte4, byte5 };
    
    Int16 value2 = BitConverter.ToInt16( bytes, 0 );
    double required_value = -1 + value2 * 6.103515625e-5;
    

    Wednesday, February 27, 2019 12:09 PM
  • Thank you this appears to have worked to an extent.

    How do I round required_value to 1 decimal place?

    Also

    How I combine 4 bytes to output in the same way as value1?

    Thank You again this was a great help.


    Alert for thread

    Wednesday, February 27, 2019 12:36 PM
  • Hi BinaryWiggles,

    Thank you for posting here.

    For your new question, you want to round required_value to 1 decimal place and combine 4 bytes to output in the same way as value1.

    You could try the following code.

                double required_value = -1 + value2 * 6.103515625e-5;
                string m = required_value.ToString("#0.0");   //round required_value to 1 decimal place
                byte[] by = { byte0, byte1, byte4, byte5 };
                int value3 = BitConverter.ToInt16(by, 0);   //combine 4 bytes to output
                Console.WriteLine(m);
                Console.WriteLine(value3);
                Console.ReadKey();
    

    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 28, 2019 5:10 AM
    Moderator