locked
Converting Int16 to bytes RRS feed

  • Question

  • All,

    I have a device that I need to send a string plus 2bytes of data.

    the data is a Int16 value.

    when i convert the value to binary i get:

    bits = 0000000101000110 which is 326.

    the device has a header of 11 characters and only accepts the string if formatted with the header plus the 2 byte value of the initial Int16 for a total of 13 bytes.

    However the header plus the bits is 14 bytes.

    How can i get this to 13 bytes.

    The device will also then send me back a message with a 8 byte header plus the new value of the sent Int16 in a 2 byte format.

    initial string

    string Finalmessage = header + (Int16)State;
    
                 byte[] cmd = Encoding.ASCII.GetBytes(Finalmessage);
    
                 ParseRecievedData(WallServer.Sendmessaage(cmd, cmd.Length),cmd.Length);


    server emulator method :

     static public byte[] Sendmessaage(byte[] msg, int numberOfBytes)
            {
    
                Console.PrintLine("Wall Server Initial legnth = " + msg.Length);
    
                byte[] r = msg.Take(13).ToArray();
    
                foreach (char t in msg)
                {
                    Console.PrintLine("data recieved == {0}", t);
                }
    
                string rx = new string(msg.Take(numberOfBytes).Select(b => (char)b).ToArray());
    
                Console.PrintLine("Wall Server Message : {0}", rx);
    
                Console.PrintLine(rx);
    
                Console.PrintLine("Wall Server Incoming legnth = " + rx.Length);
    
                byte[] bytesToUse = msg.Skip(11).Take(4).ToArray();
    
                byte[] header = Encoding.ASCII.GetBytes("COMPLETE");
    
                byte[] reply = Combine(header,bytesToUse);
    
                return (reply);
            }

    what my parser for the return data looks like:

     private static void ParseRecievedData(byte[] Rx, int numberOfBytes)   //For testing 
            {
    
    
    
                string rx = new string(Rx.Take(numberOfBytes).Select(b => (char)b).ToArray());
    
    
    
                byte[] bytes = Encoding.UTF8.GetBytes(rx);
                byte[] bytesToUse = bytes.Skip(8).Take(4).ToArray();
    
                Console.PrintLine("New Byte Array Legnth  = " + bytesToUse.Length);
    
    
                foreach (char b in bytesToUse)
                {
                    Console.PrintLine("Byte recieved == {0}", b);
                }
    
               
    
                Console.PrintLine("Incoming Message = " + rx);
    
                Console.PrintLine("Incoming legnth = " + Rx.Length);
    
                string s = System.Text.Encoding.UTF8.GetString(bytesToUse, 0, bytesToUse.Length);
    
                State = Int16.Parse(s);
    }

    i cant seem to get this to work.

    So initial string is 13 bytes(not including checksum and carriage return).

    server emulation sees 14 bytes.

    Need the value of initial Int16 (State) to always be sent as only 2 bytes.

    Hope this makes sense.

    Thank you in advance.


    Wednesday, November 11, 2020 5:45 PM

Answers

  • Hi CMCTS,

    Thank you for posting here.

    I do not quite understand you. If you just want to convert the Int16 value to two bytes, can't it be like the following?

    Did I misunderstand what you mean?

    Best Regards,

    Timon


    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.

    • Marked as answer by CMCTS Thursday, November 12, 2020 11:22 AM
    Thursday, November 12, 2020 7:54 AM

All replies

  • Hi CMCTS,

    Thank you for posting here.

    I do not quite understand you. If you just want to convert the Int16 value to two bytes, can't it be like the following?

    Did I misunderstand what you mean?

    Best Regards,

    Timon


    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.

    • Marked as answer by CMCTS Thursday, November 12, 2020 11:22 AM
    Thursday, November 12, 2020 7:54 AM
  • Yes you are correct. I spent more time working on this and it was because I was storing the value as an int not an Int16 initially. Once I corrected my code it behaved as expected. Thank you for replying.
    Thursday, November 12, 2020 11:22 AM