Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uint8-from-hex.js 1001B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. var globalThis = require('../internals/global-this');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var Uint8Array = globalThis.Uint8Array;
  5. var SyntaxError = globalThis.SyntaxError;
  6. var parseInt = globalThis.parseInt;
  7. var min = Math.min;
  8. var NOT_HEX = /[^\da-f]/i;
  9. var exec = uncurryThis(NOT_HEX.exec);
  10. var stringSlice = uncurryThis(''.slice);
  11. module.exports = function (string, into) {
  12. var stringLength = string.length;
  13. if (stringLength % 2 !== 0) throw new SyntaxError('String should be an even number of characters');
  14. var maxLength = into ? min(into.length, stringLength / 2) : stringLength / 2;
  15. var bytes = into || new Uint8Array(maxLength);
  16. var read = 0;
  17. var written = 0;
  18. while (written < maxLength) {
  19. var hexits = stringSlice(string, read, read += 2);
  20. if (exec(NOT_HEX, hexits)) throw new SyntaxError('String should only contain hex characters');
  21. bytes[written++] = parseInt(hexits, 16);
  22. }
  23. return { bytes: bytes, read: read };
  24. };