c# - How can I convert characters and string to a byte array? -


having trouble converting characters string bytes use array send through serial port.

   const char stx = '\u0002';    const char etx = '\u0003';    string pull_shelf_104 = ( stx + "01p00104##" + etx);     private byte[] wrapstring(string pull_shelf_104)    {        int length = pull_shelf_104.length;        byte[] send104 = new byte[length];        array.copy(system.text.encoding.ascii.getbytes(pull_shelf_104), 0, send104, 0, length);        return send104;    }     private void linklabel_hc1_100_linkclicked(object sender, linklabellinkclickedeventargs e)    {        if (serialport1.isopen)        {            byte data = wrapstring(string pull_shelf_104);            serialport1.write(data,0,1);        }    } 

you can try simply:

private byte[] wrapstring(string pull_shelf_104) {    return system.text.encoding.ascii.getbytes(pull_shelf_104); } 

Comments