如何从字符串中获取包括空终止符的字节? [英] How do I get bytes including a null termination char from a string?
本文介绍了如何从字符串中获取包括空终止符的字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好StackOverflowers
Hello StackOverflowers,
我在某处读到C#字符串不是以null结尾的.很好!但我想知道该方法是否:
I read somewhere that C# string are not null terminated. M'fine ! But I'd like to know if the method :
byte[] myBytes = Encoding.ASCII.GetBytes(myString);
在数组的末尾添加一个空终止符,或者是否必须手动执行?(将使用此数组的C系统确实需要它).
add a null termination character to the end of the array or if I must do it manually ? (the C system which will use this array does need it).
如果是手动的,我想方法是:
If manual, I guess the way would be :
byte[] myBytes = Encoding.ASCII.GetBytes(myString + '\0');
数据应为ASCII,无需担心编码.
The data is meant to be ASCII, no worries about encoding.
推荐答案
它没有添加,您的第二个代码将起作用.
It doesn't add, your second code will work.
byte[] myBytes =
Encoding.ASCII.GetBytes("A"); // gives {65}
byte[] myBytes =
Encoding.ASCII.GetBytes("A"+"\0"); // gives {65, 0}
这篇关于如何从字符串中获取包括空终止符的字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文