Building a new variable from a byte list












1















I am trying to write a data logger GUI. This application receives information over serial port about the variables that are defined in an embedded software.



I have a class for storing these variables. A new instance of this class is created everytime a new variable is requested in watch-window.



public class Var_t
{
public string vType; //variable type chosen from a combobox
public string vName; //variable name read from the serial port
public UInt32 vAddr; //variable ram addr read from the serial port
public byte vSize; //variable size read from the serial port
public List<byte> vBuffer; //variable content buffer
};


Since the variable size and type are read dynamically at the runtime, I'm just filling the byte List while receiving the value of this variable.



I need a class that combines these bytes using the vType datatype after the transfer is completed. This class will return a string to let me show the variable value in a combo-box to the user:



public class BuildVariable(List<byte> varBuf, string varType)
{
string ValueStr;

//I tried using BitConverter to combine the bytes into a new object
//but it does not accept any parameter for the type

return ValueStr;
};


Possible variable types are byte, uint8, int8, uint16, int16, uint32, int32, uint64, int64, float, double,char,string










share|improve this question























  • Simplest way is to simply switch on vType or use a dictionary with vType as key & a BitConverter lambda as value.

    – Alex K.
    Nov 15 '18 at 11:15











  • I'm not very experienced in C# actually. I know how and why we use the dictionary but I don't know much about lambda. Would you please give me an example or a reference about what you mean? Do you think I should add an example to my question?

    – abdullah cinar
    Nov 15 '18 at 11:30
















1















I am trying to write a data logger GUI. This application receives information over serial port about the variables that are defined in an embedded software.



I have a class for storing these variables. A new instance of this class is created everytime a new variable is requested in watch-window.



public class Var_t
{
public string vType; //variable type chosen from a combobox
public string vName; //variable name read from the serial port
public UInt32 vAddr; //variable ram addr read from the serial port
public byte vSize; //variable size read from the serial port
public List<byte> vBuffer; //variable content buffer
};


Since the variable size and type are read dynamically at the runtime, I'm just filling the byte List while receiving the value of this variable.



I need a class that combines these bytes using the vType datatype after the transfer is completed. This class will return a string to let me show the variable value in a combo-box to the user:



public class BuildVariable(List<byte> varBuf, string varType)
{
string ValueStr;

//I tried using BitConverter to combine the bytes into a new object
//but it does not accept any parameter for the type

return ValueStr;
};


Possible variable types are byte, uint8, int8, uint16, int16, uint32, int32, uint64, int64, float, double,char,string










share|improve this question























  • Simplest way is to simply switch on vType or use a dictionary with vType as key & a BitConverter lambda as value.

    – Alex K.
    Nov 15 '18 at 11:15











  • I'm not very experienced in C# actually. I know how and why we use the dictionary but I don't know much about lambda. Would you please give me an example or a reference about what you mean? Do you think I should add an example to my question?

    – abdullah cinar
    Nov 15 '18 at 11:30














1












1








1








I am trying to write a data logger GUI. This application receives information over serial port about the variables that are defined in an embedded software.



I have a class for storing these variables. A new instance of this class is created everytime a new variable is requested in watch-window.



public class Var_t
{
public string vType; //variable type chosen from a combobox
public string vName; //variable name read from the serial port
public UInt32 vAddr; //variable ram addr read from the serial port
public byte vSize; //variable size read from the serial port
public List<byte> vBuffer; //variable content buffer
};


Since the variable size and type are read dynamically at the runtime, I'm just filling the byte List while receiving the value of this variable.



I need a class that combines these bytes using the vType datatype after the transfer is completed. This class will return a string to let me show the variable value in a combo-box to the user:



public class BuildVariable(List<byte> varBuf, string varType)
{
string ValueStr;

//I tried using BitConverter to combine the bytes into a new object
//but it does not accept any parameter for the type

return ValueStr;
};


Possible variable types are byte, uint8, int8, uint16, int16, uint32, int32, uint64, int64, float, double,char,string










share|improve this question














I am trying to write a data logger GUI. This application receives information over serial port about the variables that are defined in an embedded software.



I have a class for storing these variables. A new instance of this class is created everytime a new variable is requested in watch-window.



public class Var_t
{
public string vType; //variable type chosen from a combobox
public string vName; //variable name read from the serial port
public UInt32 vAddr; //variable ram addr read from the serial port
public byte vSize; //variable size read from the serial port
public List<byte> vBuffer; //variable content buffer
};


Since the variable size and type are read dynamically at the runtime, I'm just filling the byte List while receiving the value of this variable.



I need a class that combines these bytes using the vType datatype after the transfer is completed. This class will return a string to let me show the variable value in a combo-box to the user:



public class BuildVariable(List<byte> varBuf, string varType)
{
string ValueStr;

//I tried using BitConverter to combine the bytes into a new object
//but it does not accept any parameter for the type

return ValueStr;
};


Possible variable types are byte, uint8, int8, uint16, int16, uint32, int32, uint64, int64, float, double,char,string







c# arrays list types type-conversion






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 11:06









abdullah cinarabdullah cinar

178314




178314













  • Simplest way is to simply switch on vType or use a dictionary with vType as key & a BitConverter lambda as value.

    – Alex K.
    Nov 15 '18 at 11:15











  • I'm not very experienced in C# actually. I know how and why we use the dictionary but I don't know much about lambda. Would you please give me an example or a reference about what you mean? Do you think I should add an example to my question?

    – abdullah cinar
    Nov 15 '18 at 11:30



















  • Simplest way is to simply switch on vType or use a dictionary with vType as key & a BitConverter lambda as value.

    – Alex K.
    Nov 15 '18 at 11:15











  • I'm not very experienced in C# actually. I know how and why we use the dictionary but I don't know much about lambda. Would you please give me an example or a reference about what you mean? Do you think I should add an example to my question?

    – abdullah cinar
    Nov 15 '18 at 11:30

















Simplest way is to simply switch on vType or use a dictionary with vType as key & a BitConverter lambda as value.

– Alex K.
Nov 15 '18 at 11:15





Simplest way is to simply switch on vType or use a dictionary with vType as key & a BitConverter lambda as value.

– Alex K.
Nov 15 '18 at 11:15













I'm not very experienced in C# actually. I know how and why we use the dictionary but I don't know much about lambda. Would you please give me an example or a reference about what you mean? Do you think I should add an example to my question?

– abdullah cinar
Nov 15 '18 at 11:30





I'm not very experienced in C# actually. I know how and why we use the dictionary but I don't know much about lambda. Would you please give me an example or a reference about what you mean? Do you think I should add an example to my question?

– abdullah cinar
Nov 15 '18 at 11:30












1 Answer
1






active

oldest

votes


















0














This is what I ended up with:



public static string GetTypedString(Var_t varToDisp)
{
string ValueDisplay;

switch (varToDisp.vType)
{
case "byte":

lock (varToDisp.value)
{
byte temp_byte = varToDisp.vBuffer[0];
ValueDisplay = temp_byte.ToString();
}
break;

case "uint16":

lock (varToDisp.value)
{
UInt16 temp_ui16 = BitConverter.ToUInt16(varToDisp.vBuffer.ToArray<byte>(), 0);
ValueDisplay = temp_ui16.ToString();
}
break;

case "uint32":

lock (varToDisp.value)
{
UInt32 temp_uint32 = BitConverter.ToUInt32(varToDisp.vBuffer.ToArray<byte>(), 0);
ValueDisplay = temp_uint32.ToString();
}
break;

//and so on..
}

return ValueDisplay;
}


I couldn't find a way to do it with a dynamic type selection, so I had to do the conversion one by one.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318077%2fbuilding-a-new-variable-from-a-byte-list%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is what I ended up with:



    public static string GetTypedString(Var_t varToDisp)
    {
    string ValueDisplay;

    switch (varToDisp.vType)
    {
    case "byte":

    lock (varToDisp.value)
    {
    byte temp_byte = varToDisp.vBuffer[0];
    ValueDisplay = temp_byte.ToString();
    }
    break;

    case "uint16":

    lock (varToDisp.value)
    {
    UInt16 temp_ui16 = BitConverter.ToUInt16(varToDisp.vBuffer.ToArray<byte>(), 0);
    ValueDisplay = temp_ui16.ToString();
    }
    break;

    case "uint32":

    lock (varToDisp.value)
    {
    UInt32 temp_uint32 = BitConverter.ToUInt32(varToDisp.vBuffer.ToArray<byte>(), 0);
    ValueDisplay = temp_uint32.ToString();
    }
    break;

    //and so on..
    }

    return ValueDisplay;
    }


    I couldn't find a way to do it with a dynamic type selection, so I had to do the conversion one by one.






    share|improve this answer




























      0














      This is what I ended up with:



      public static string GetTypedString(Var_t varToDisp)
      {
      string ValueDisplay;

      switch (varToDisp.vType)
      {
      case "byte":

      lock (varToDisp.value)
      {
      byte temp_byte = varToDisp.vBuffer[0];
      ValueDisplay = temp_byte.ToString();
      }
      break;

      case "uint16":

      lock (varToDisp.value)
      {
      UInt16 temp_ui16 = BitConverter.ToUInt16(varToDisp.vBuffer.ToArray<byte>(), 0);
      ValueDisplay = temp_ui16.ToString();
      }
      break;

      case "uint32":

      lock (varToDisp.value)
      {
      UInt32 temp_uint32 = BitConverter.ToUInt32(varToDisp.vBuffer.ToArray<byte>(), 0);
      ValueDisplay = temp_uint32.ToString();
      }
      break;

      //and so on..
      }

      return ValueDisplay;
      }


      I couldn't find a way to do it with a dynamic type selection, so I had to do the conversion one by one.






      share|improve this answer


























        0












        0








        0







        This is what I ended up with:



        public static string GetTypedString(Var_t varToDisp)
        {
        string ValueDisplay;

        switch (varToDisp.vType)
        {
        case "byte":

        lock (varToDisp.value)
        {
        byte temp_byte = varToDisp.vBuffer[0];
        ValueDisplay = temp_byte.ToString();
        }
        break;

        case "uint16":

        lock (varToDisp.value)
        {
        UInt16 temp_ui16 = BitConverter.ToUInt16(varToDisp.vBuffer.ToArray<byte>(), 0);
        ValueDisplay = temp_ui16.ToString();
        }
        break;

        case "uint32":

        lock (varToDisp.value)
        {
        UInt32 temp_uint32 = BitConverter.ToUInt32(varToDisp.vBuffer.ToArray<byte>(), 0);
        ValueDisplay = temp_uint32.ToString();
        }
        break;

        //and so on..
        }

        return ValueDisplay;
        }


        I couldn't find a way to do it with a dynamic type selection, so I had to do the conversion one by one.






        share|improve this answer













        This is what I ended up with:



        public static string GetTypedString(Var_t varToDisp)
        {
        string ValueDisplay;

        switch (varToDisp.vType)
        {
        case "byte":

        lock (varToDisp.value)
        {
        byte temp_byte = varToDisp.vBuffer[0];
        ValueDisplay = temp_byte.ToString();
        }
        break;

        case "uint16":

        lock (varToDisp.value)
        {
        UInt16 temp_ui16 = BitConverter.ToUInt16(varToDisp.vBuffer.ToArray<byte>(), 0);
        ValueDisplay = temp_ui16.ToString();
        }
        break;

        case "uint32":

        lock (varToDisp.value)
        {
        UInt32 temp_uint32 = BitConverter.ToUInt32(varToDisp.vBuffer.ToArray<byte>(), 0);
        ValueDisplay = temp_uint32.ToString();
        }
        break;

        //and so on..
        }

        return ValueDisplay;
        }


        I couldn't find a way to do it with a dynamic type selection, so I had to do the conversion one by one.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 7:39









        abdullah cinarabdullah cinar

        178314




        178314






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318077%2fbuilding-a-new-variable-from-a-byte-list%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory