public static unsafe string ByteArrayToEtagString(byte[] byteArray) { var results = new string('-', 36); fixed(byte *restarts = byteArray) fixed(char *buf = results) { int fst = (*restarts << 24) | (*(restarts + 1) << 16) | (*(restarts + 2) << 8) | (*(restarts + 3)); int snd = (*(restarts + 4) << 24) | (*(restarts + 5) << 16) | (*(restarts + 6) << 8) | (*(restarts + 7)); var etagRestarts = (uint)snd | ((long)fst << 32); var changes = restarts + 8; fst = (*changes << 24) | (*(changes + 1) << 16) | (*(changes + 2) << 8) | (*(changes + 3)); snd = (*(changes + 4) << 24) | (*(changes + 5) << 16) | (*(changes + 6) << 8) | (*(changes + 7)); var etagChanges = (uint)snd | ((long)fst << 32); var bytes = new LongBytes { Long = etagRestarts }; *(int *)(&buf[0]) = ByteToHexStringAsInt32Lookup[bytes.Byte7]; *(int *)(&buf[2]) = ByteToHexStringAsInt32Lookup[bytes.Byte6]; *(int *)(&buf[4]) = ByteToHexStringAsInt32Lookup[bytes.Byte5]; *(int *)(&buf[6]) = ByteToHexStringAsInt32Lookup[bytes.Byte4]; //buf[8] = '-'; *(int *)(&buf[9]) = ByteToHexStringAsInt32Lookup[bytes.Byte3]; *(int *)(&buf[11]) = ByteToHexStringAsInt32Lookup[bytes.Byte2]; //buf[13] = '-'; *(int *)(&buf[14]) = ByteToHexStringAsInt32Lookup[bytes.Byte1]; *(int *)(&buf[16]) = ByteToHexStringAsInt32Lookup[bytes.Byte0]; //buf[18] = '-'; bytes.Long = etagChanges; *(int *)(&buf[19]) = ByteToHexStringAsInt32Lookup[bytes.Byte7]; *(int *)(&buf[21]) = ByteToHexStringAsInt32Lookup[bytes.Byte6]; //buf[23] = '-'; *(int *)(&buf[24]) = ByteToHexStringAsInt32Lookup[bytes.Byte5]; *(int *)(&buf[26]) = ByteToHexStringAsInt32Lookup[bytes.Byte4]; *(int *)(&buf[28]) = ByteToHexStringAsInt32Lookup[bytes.Byte3]; *(int *)(&buf[30]) = ByteToHexStringAsInt32Lookup[bytes.Byte2]; *(int *)(&buf[32]) = ByteToHexStringAsInt32Lookup[bytes.Byte1]; *(int *)(&buf[34]) = ByteToHexStringAsInt32Lookup[bytes.Byte0]; return(results); } }
public override unsafe string ToString() { var results = new string('-', 36); // Optimized with the help of Oliver Hallam ([email protected]) fixed(char *buf = results) { var bytes = new LongBytes { Long = this.restarts }; *(int *)(&buf[0]) = ByteToHexStringAsInt32Lookup[bytes.Byte7]; *(int *)(&buf[2]) = ByteToHexStringAsInt32Lookup[bytes.Byte6]; *(int *)(&buf[4]) = ByteToHexStringAsInt32Lookup[bytes.Byte5]; *(int *)(&buf[6]) = ByteToHexStringAsInt32Lookup[bytes.Byte4]; //buf[8] = '-'; *(int *)(&buf[9]) = ByteToHexStringAsInt32Lookup[bytes.Byte3]; *(int *)(&buf[11]) = ByteToHexStringAsInt32Lookup[bytes.Byte2]; //buf[13] = '-'; *(int *)(&buf[14]) = ByteToHexStringAsInt32Lookup[bytes.Byte1]; *(int *)(&buf[16]) = ByteToHexStringAsInt32Lookup[bytes.Byte0]; //buf[18] = '-'; bytes.Long = this.changes; *(int *)(&buf[19]) = ByteToHexStringAsInt32Lookup[bytes.Byte7]; *(int *)(&buf[21]) = ByteToHexStringAsInt32Lookup[bytes.Byte6]; //buf[23] = '-'; *(int *)(&buf[24]) = ByteToHexStringAsInt32Lookup[bytes.Byte5]; *(int *)(&buf[26]) = ByteToHexStringAsInt32Lookup[bytes.Byte4]; *(int *)(&buf[28]) = ByteToHexStringAsInt32Lookup[bytes.Byte3]; *(int *)(&buf[30]) = ByteToHexStringAsInt32Lookup[bytes.Byte2]; *(int *)(&buf[32]) = ByteToHexStringAsInt32Lookup[bytes.Byte1]; *(int *)(&buf[34]) = ByteToHexStringAsInt32Lookup[bytes.Byte0]; return(results); } }