index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. // selected so (BASE - 1) * 0x100000000 + 0xffffffff is a safe integer
  3. var BASE = 1000000;
  4. function readInt8(buffer) {
  5. var high = buffer.readInt32BE(0);
  6. var low = buffer.readUInt32BE(4);
  7. var sign = '';
  8. if (high < 0) {
  9. high = ~high + (low === 0);
  10. low = (~low + 1) >>> 0;
  11. sign = '-';
  12. }
  13. var result = '';
  14. var carry;
  15. var t;
  16. var digits;
  17. var pad;
  18. var l;
  19. var i;
  20. {
  21. carry = high % BASE;
  22. high = high / BASE >>> 0;
  23. t = 0x100000000 * carry + low;
  24. low = t / BASE >>> 0;
  25. digits = '' + (t - BASE * low);
  26. if (low === 0 && high === 0) {
  27. return sign + digits + result;
  28. }
  29. pad = '';
  30. l = 6 - digits.length;
  31. for (i = 0; i < l; i++) {
  32. pad += '0';
  33. }
  34. result = pad + digits + result;
  35. }
  36. {
  37. carry = high % BASE;
  38. high = high / BASE >>> 0;
  39. t = 0x100000000 * carry + low;
  40. low = t / BASE >>> 0;
  41. digits = '' + (t - BASE * low);
  42. if (low === 0 && high === 0) {
  43. return sign + digits + result;
  44. }
  45. pad = '';
  46. l = 6 - digits.length;
  47. for (i = 0; i < l; i++) {
  48. pad += '0';
  49. }
  50. result = pad + digits + result;
  51. }
  52. {
  53. carry = high % BASE;
  54. high = high / BASE >>> 0;
  55. t = 0x100000000 * carry + low;
  56. low = t / BASE >>> 0;
  57. digits = '' + (t - BASE * low);
  58. if (low === 0 && high === 0) {
  59. return sign + digits + result;
  60. }
  61. pad = '';
  62. l = 6 - digits.length;
  63. for (i = 0; i < l; i++) {
  64. pad += '0';
  65. }
  66. result = pad + digits + result;
  67. }
  68. {
  69. carry = high % BASE;
  70. t = 0x100000000 * carry + low;
  71. digits = '' + t % BASE;
  72. return sign + digits + result;
  73. }
  74. }
  75. module.exports = readInt8;