in c# how to convert an varto a char array. char[8] array I can guarantee this var is not too big.
such as, I have
var a = 634711440648369988;
and I want char[8] c
store it.
How to do this conversion correctly? Thanks.
您可以将其转换为字符串并使用ToCharArray
方法。
char [ ] c = a.ToString().ToCharArray();
In C# there is no such thing as char[8]
. An array of char
would be 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[]
. Note that you want byte[]
rather than char[]
since char
in C# is a 16 bit data type.
You can obtain what you need by calling 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 .
int a = 123412;
char[] d = a.ToString().ToCharArray();
foreach (char c in d)
{
Console.WriteLine(c);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.