Created
February 23, 2012 20:13
-
-
Save rpgmaker/1894839 to your computer and use it in GitHub Desktop.
Int64ToBytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static byte[] Int64ToBytes(long value) { | |
if (value <= 255) return new byte[] { (byte)value }; | |
const int msb = 0xff; | |
byte[] buffer = new byte[8]; | |
var index = 0; | |
var sValue = 0L; | |
unchecked { | |
buffer[index++] = (byte)(value & msb); | |
if ((sValue = (value >> 8) & msb) > 0) { | |
buffer[index++] = (byte)sValue; | |
if ((sValue = (value >> 16) & msb) > 0) { | |
buffer[index++] = (byte)sValue; | |
if ((sValue = (value >> 24) & msb) > 0) { | |
buffer[index++] = (byte)sValue; | |
if ((sValue = (value >> 32) & msb) > 0) { | |
buffer[index++] = (byte)sValue; | |
if ((sValue = (value >> 40) & msb) > 0) { | |
buffer[index++] = (byte)sValue; | |
if ((sValue = (value >> 48) & msb) > 0) { | |
buffer[index++] = (byte)sValue; | |
if ((sValue = (value >> 56) & msb) > 0) | |
buffer[index++] = (byte)sValue; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
var buffer2 = new byte[index]; | |
Buffer.BlockCopy(buffer, 0, buffer2, 0, index); | |
return buffer2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment