none
Convert byte array to Int16 RRS feed

  • Question

  • I am using an external DLL written in C to read out memory. The function returns an array of bytes.

     

    Code Snippet

    Byte[] buf = new Byte[2];

    DLLFunctionCall(opt1, opt2, buf);

     

     

    Next I need to convert the reply to an Int16. Now, the samples provided with the DLL only convert to a string, so I was thinking something alike:

     

    Code Snippet
    Int16 val = Convert.ToInt16(System.Text.Encoding.ASCII.GetString(buf));

     

     

    This works when the buf variable contains a value, but if it's empty (contains 0's) the converted string returns'\0\0'. When I try to convert this string to the Int16 variable I get an exception saying the input format was not expected.

     

    Any idea how I can convert this properly or what I'm doing wrong?

    Friday, February 15, 2008 2:00 PM

Answers

  • BitConverter is your friend when it comes to converting to and from byte arrays:

     

    short value = BitConverter.ToInt16(buffer, 0);

     

    The only one that is a little difficult is strings because conversion of a byte array to/from a string results in this: AB-CD-EF.  Instead to convert to and from strings with a byte array use Encoding.  Either Ascii or Unicode depending on your circumstance.

     

    Michael Taylor - 2/15/08

    http://p3net.mvps.org

     

     

    Friday, February 15, 2008 2:17 PM
    Moderator
  • If your byte array is actually the data as a string then you first need to convert it to a string and then use ConvertBitConverter is used to convert the raw data to its translated format.  A -1 would be FF.

     

    If your byte array contains the equivalent of "-1" then do this:

     

    string str = Encoding.Ascii.GetString(buffer, ...);

    short value = Convert.ToInt16(str);

     

    If the byte array contains FF then do this:

     

    short value = BitConveter.ToInt16(buffer, 0);

     

    Michael Taylor - 2/15/08

    http://p3net.mvps.org

     

    Friday, February 15, 2008 2:41 PM
    Moderator

All replies

  • BitConverter is your friend when it comes to converting to and from byte arrays:

     

    short value = BitConverter.ToInt16(buffer, 0);

     

    The only one that is a little difficult is strings because conversion of a byte array to/from a string results in this: AB-CD-EF.  Instead to convert to and from strings with a byte array use Encoding.  Either Ascii or Unicode depending on your circumstance.

     

    Michael Taylor - 2/15/08

    http://p3net.mvps.org

     

     

    Friday, February 15, 2008 2:17 PM
    Moderator
  • The BitConverter class seems to work in this case, however it causes other issues. At one point I am expecting -1 as a result and when I use the ASCII encoding functionality and convert that string to Int16 I get -1. When I use the BitConverter.ToInt16() function I get 12589.

     

    At this point the byte array contains [0] = 45 and [1] = 49.

    Friday, February 15, 2008 2:26 PM
  • If your byte array is actually the data as a string then you first need to convert it to a string and then use ConvertBitConverter is used to convert the raw data to its translated format.  A -1 would be FF.

     

    If your byte array contains the equivalent of "-1" then do this:

     

    string str = Encoding.Ascii.GetString(buffer, ...);

    short value = Convert.ToInt16(str);

     

    If the byte array contains FF then do this:

     

    short value = BitConveter.ToInt16(buffer, 0);

     

    Michael Taylor - 2/15/08

    http://p3net.mvps.org

     

    Friday, February 15, 2008 2:41 PM
    Moderator