[英]in c# how to convert an var to a char array. char array[8]
in c# how to convert an varto a char array. 在C#中如何将varto转换为char数组。 char[8] array I can guarantee this var is not too big.
char [8]数组我可以保证此var不会太大。
such as, I have 例如,我有
var a = 634711440648369988;
and I want char[8] c
store it. 我想用
char[8] c
存储它。
How to do this conversion correctly? 如何正确进行此转换? Thanks.
谢谢。
您可以将其转换为字符串并使用ToCharArray
方法。
char [ ] c = a.ToString().ToCharArray();
In C# there is no such thing as char[8]
. 在C#中,没有诸如
char[8]
这样的东西。 An array of char
would be char[]
. char
数组将为char[]
。
I guess you are coming at this from a C++ viewpoint and actually want an array of bytes, of length 8. In which case the type you need is byte[]
. 我想您是从C ++的角度出发的,实际上是要一个长度为8的字节数组。在这种情况下,您需要的类型是
byte[]
。 Note that you want byte[]
rather than char[]
since char
in C# is a 16 bit data type. 请注意,由于C#中的
char
是16位数据类型,因此您需要byte[]
而不是char[]
。
You can obtain what you need by calling BitConverter.GetBytes()
. 您可以通过调用
BitConverter.GetBytes()
获得所需的内容。 When you call this function passing an 8 byte integer, the returned array will be a byte[]
with length equal to 8, as stated in the documentation . 当您调用此函数传递8个字节的整数时,返回的数组将是一个
byte[]
,其长度等于8,如文档所述 。
int a = 123412;
char[] d = a.ToString().ToCharArray();
foreach (char c in d)
{
Console.WriteLine(c);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.