bytesToUuid.js 1002 B

123456789101112131415161718
  1. /**
  2. * Convert array of 16 byte values to UUID string format of the form:
  3. * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  4. */
  5. var byteToHex = [];
  6. for (var i = 0; i < 256; ++i) {
  7. byteToHex.push((i + 0x100).toString(16).substr(1));
  8. }
  9. function bytesToUuid(buf, offset_) {
  10. var offset = offset_ || 0; // Note: Be careful editing this code! It's been tuned for performance
  11. // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
  12. return (byteToHex[buf[offset + 0]] + byteToHex[buf[offset + 1]] + byteToHex[buf[offset + 2]] + byteToHex[buf[offset + 3]] + '-' + byteToHex[buf[offset + 4]] + byteToHex[buf[offset + 5]] + '-' + byteToHex[buf[offset + 6]] + byteToHex[buf[offset + 7]] + '-' + byteToHex[buf[offset + 8]] + byteToHex[buf[offset + 9]] + '-' + byteToHex[buf[offset + 10]] + byteToHex[buf[offset + 11]] + byteToHex[buf[offset + 12]] + byteToHex[buf[offset + 13]] + byteToHex[buf[offset + 14]] + byteToHex[buf[offset + 15]]).toLowerCase();
  13. }
  14. export default bytesToUuid;