uuid-convert

Convert UUID between string, hexadecimal string and byte array

Usage no npm install needed!

<script type="module">
  import uuidConvert from 'https://cdn.skypack.dev/uuid-convert';
</script>

README

uuid-convert

A simple tool for coverting UUIDs between string, hexadecimal string and byte array.

Installation and Usage

Install the library with npm install -S uuid-convert

const uuidConvert = require('uuid-convert')

API

byteArrayToUuid(uuidBuffer[, offset])

Convert byte array to uuid string

  • uuidBuffer - (Array) UUID representation in byte array.
  • offset - (Number) Starting point in uuidBuffer. Default: 0

byteArrayToHex(uuidBuffer[, offset[, hexPrefix]])

Convert byte array to hexadecimal string

  • uuidBuffer - (Array) UUID representation in byte array.
  • offset - (Number) Starting point in uuidBuffer. Default: 0
  • hexPrefix - (Boolean) Add the 0x prefix. Default: false

hexToUuid(hexString)

Convert hexadecimal string to uuid string

  • hexString - (String) Hexadecimal string.

uuidToHex(uuidString[, hexPrefix])

Convert uuid string to hexadecimal string

  • uuidString - (String) UUID string.
  • hexPrefix - (Boolean) Add the 0x prefix. Default: false

uuidToHex(uuidOrHexString[, uuidBuffer[, offset]])

Convert uuid string or hexadecimal string to byte array

  • uuidOrHexString - (String) UUID or hexadecimal string.
  • uuidBuffer - (Array) UUID representation in byte array.
  • offset - (Number) Starting point in uuidBuffer. Default: 0

Examples

uuidConvert.byteArrayToUuid([0x5f, 0x3a, 0x2a, 0xfe, 0x56, 0xab, 0x11, 0xe9, 0x86, 0x47, 0xd6, 0x63, 0xbd, 0x87, 0x3d, 0x93, 0xff])
// 5f3a2afe-56ab-11e9-8647-d663bd873d93
uuidConvert.byteArrayToHex([0x00, 0xc0, 0x18, 0xed, 0xea, 0xc0, 0x2f, 0x4d, 0xde, 0x82, 0xec, 0xdb, 0x06, 0x8d, 0x9e, 0x6f, 0x65], 1, true)
// c018edeac02f4dde82ecdb068d9e6f65
uuidConvert.hexToUuid('0xc018edeac02f4dde82ecdb068d9e6f65')
// c018edea-c02f-4dde-82ec-db068d9e6f65
uuidConvert.uuidToHex('5f3a2afe-56ab-11e9-8647-d663bd873d93')
// 5f3a2afe56ab11e98647d663bd873d93
uuidConvert.toByteArray('5f3a2afe-56ab-11e9-8647-d663bd873d93')
// [0x5f, 0x3a, 0x2a, 0xfe, 0x56, 0xab, 0x11, 0xe9, 0x86, 0x47, 0xd6, 0x63, 0xbd, 0x87, 0x3d, 0x93]
uuidConvert.toByteArray('c018edeac02f4dde82ecdb068d9e6f65')
// [0xc0, 0x18, 0xed, 0xea, 0xc0, 0x2f, 0x4d, 0xde, 0x82, 0xec, 0xdb, 0x06, 0x8d, 0x9e, 0x6f, 0x65]

Tests

Tests are using mocha, to run the tests use npm test