如何将一个值类型在C#中的byte []转换? [英] how to convert a value type to byte[] in C#?
本文介绍了如何将一个值类型在C#中的byte []转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的这个等价的:
I want to do the equivalent of this:
byte[] byteArray;
enum commands : byte {one, two};
commands content = one;
byteArray = (byte*)&content;
是的,它现在是一个字节,但是考虑到我想改变它的未来?我怎么做的字节数组包含内容? (我不在乎复制它)。
yes, it's a byte now, but consider I want to change it in the future? how do I make byteArray contain content? (I don't care to copy it).
推荐答案
BitConverter 类可能是你在找什么。例如:
The BitConverter class might be what you are looking for. Example:
int input = 123;
byte[] output = BitConverter.GetBytes(input);
如果您枚举被称为是一个Int32派生类型,你可以简单地先投它的值:
If your enum was known to be an Int32 derived type, you could simply cast its values first:
BitConverter.GetBytes((int)commands.one);
这篇关于如何将一个值类型在C#中的byte []转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文