Skip to content

Instantly share code, notes, and snippets.

@rpgmaker
Created February 23, 2012 20:13
Show Gist options
  • Save rpgmaker/1894839 to your computer and use it in GitHub Desktop.
Save rpgmaker/1894839 to your computer and use it in GitHub Desktop.
Int64ToBytes
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