需要帮助转换

时间:2011-05-02 04:07:40

标签: c# c++

您需要帮助才能将此C ++代码转换为C#

sprintf((char *)(dataBuffer), "Failed statistics read, device %s", device); 

dataBuffer是byte []

我写了这个,但错误地将字符串转换为byte []

  dataBuffer = string.Format("Failed statistics read, device {0}", device);

1 个答案:

答案 0 :(得分:2)

String str = string.Format("Failed statistics read, device {0}", device);
byte[] dataBuffer = System.Text.Encoding.ASCII.GetBytes(str);
// for 2-byte unicode
byte[] dataBuffer = System.Text.Encoding.Unicode.GetBytes(str);
// for UTF8 unicode
byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(str);