decimalToHexString.js 584 B

12345678910111213141516171819202122232425
  1. // Copyright (C) 2017 André Bargull. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: |
  5. Collection of functions used to assert the correctness of various encoding operations.
  6. ---*/
  7. function decimalToHexString(n) {
  8. var hex = "0123456789ABCDEF";
  9. n >>>= 0;
  10. var s = "";
  11. while (n) {
  12. s = hex[n & 0xf] + s;
  13. n >>>= 4;
  14. }
  15. while (s.length < 4) {
  16. s = "0" + s;
  17. }
  18. return s;
  19. }
  20. function decimalToPercentHexString(n) {
  21. var hex = "0123456789ABCDEF";
  22. return "%" + hex[(n >> 4) & 0xf] + hex[n & 0xf];
  23. }