unity 字节流转string
时间: 2023-09-11 21:13:20 浏览: 112
Unity调用打印机打印图片
在Unity中将字节流转换为字符串的方法可以通过以下代码实现:
```
public static string BytesToString(byte[] bytes)
{
if (bytes == null || bytes.Length == 0)
{
return "";
}
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
{
sb.Append(b.ToString("X2"));
sb.Append(" ");
}
return sb.ToString().Trim();
}
```
这个方法接受一个字节数组作为参数,然后将每个字节转换为对应的十六进制字符串,并用空格分隔。最后返回转换后的字符串。你可以直接调用这个方法来实现字节流转换为字符串的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Unity3D 串口通讯---字符串转字节流](https://blog.csdn.net/WenHuiJun_/article/details/122943565)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Unity C# 字节数组byte[]转字符串string中文乱码(utf8无效)的问题](https://blog.csdn.net/qq_33789001/article/details/115769133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文