C# 在短字节和字节之间转换的好方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1442583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good way to convert between short and bytes?
提问by RCIX
I need to take pairs of bytes in, and output shorts, and take shorts in and output pairs of bytes. Here are the functions i've devised for such a purpose:
我需要输入成对的字节并输出短片,并输入短片并输出成对的字节。以下是我为此目的设计的功能:
static short ToShort(short byte1, short byte2)
{
short number = (short)byte2;
number <<= 4;
number += (short)byte1;
return number;
}
static void FromShort(short number, out byte byte1, out byte byte2)
{
byte byte2 = (byte)(number >> 4);
short tempByte = (short)byte2 << 4;
byte byte1 = (byte)(number - tempByte);
}
I think this is correct but i'm not sure. If this isn't the right way to do it, what is? is there a way to do this already in the framework?
我认为这是正确的,但我不确定。如果这不是正确的方法,那是什么?有没有办法在框架中做到这一点?
采纳答案by Ates Goral
Shorter version (also shifting 8 bits instead of 4):
较短的版本(也移动 8 位而不是 4 位):
static short ToShort(short byte1, short byte2)
{
return (byte2 << 8) + byte1;
}
static void FromShort(short number, out byte byte1, out byte byte2)
{
byte2 = (byte)(number >> 8);
byte1 = (byte)(number & 255);
}
回答by John Kugelman
Bytes are 8 bits, not 4, so your shifting is off. You also declared local variables in the second function so you wouldn't end up writing the the out
parameters like you intend. It's also clearer/better if you limit yourself to bitwise operations (&
, |
, and ~
) where possible.
字节是 8 位,而不是 4 位,因此您的移位已关闭。您还在第二个函数中声明了局部变量,因此您最终不会out
像预期的那样编写参数。如果您尽可能将自己限制为按位运算(&
、|
、 和~
),它也会更清晰/更好。
static short ToShort(byte byte1, byte byte2)
{
return (short) ((byte2 << 8) | (byte1 << 0));
}
static void FromShort(short number, out byte byte1, out byte byte2)
{
byte2 = (byte) (number >> 8);
byte1 = (byte) (number >> 0);
}
Note that the left and right shifts by zero are unnecessary, strictly speaking. I just put those in for symmetry. Also, personally I'd recommend you just learn bitwise arithmetic cold and skip writing helper functions like these. No need to hide the details with something so fundamental, IMHO.
请注意,严格来说,左移和右移零是不必要的。我只是为了对称而把它们放进去。另外,我个人建议您只学习按位算术冷,并跳过编写此类辅助函数。恕我直言,无需用如此基本的东西隐藏细节。
回答by Marc Gravell
If you want to take bytes... take bytes; and your shifts are off, and |
would be more intuitive:
如果你想拿字节......拿字节; 并且您的班次已关闭,并且|
会更直观:
static short ToShort(byte byte1, byte byte2)
{ // using Int32 because that is what all the operations return anyway...
return (short)((((int)byte1) << 8) | (int)byte2);
}
static void FromShort(short number, out byte byte1, out byte byte2)
{
byte1 = (byte)(number >> 8); // to treat as same byte 1 from above
byte2 = (byte)number;
}
回答by Marc Gravell
System.BitConverter
System.BitConverter
回答by TJB
Use BitConverter
short number = 42;
byte[] numberBytes = BitConverter.GetBytes(number);
short converted = BitConverter.ToInt16(numberBytes);