C# convert string into its byte[] equivalent

后端 未结 6 1806
北恋
北恋 2021-01-05 06:10

At this point most people will be thinking \"Ah ill post this..:\"

byte[] dataB= System.Text.Encoding.ASCII.GetBytes(data);

However.. the p

6条回答
  •  无人及你
    2021-01-05 06:55

    //convert a string to a byte array

    public static byte[] StrToByteArray(string str)
    {
        System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
        return encoding.GetBytes(str);
    }
    

    //convert a byte array to a string

    public string ByteArrayToStr(byte [] dBytes)
    {
    System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
    return enc.GetString(dBytes);
    }
    

提交回复
热议问题