Guest User

Untitled

a guest
Feb 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. function toIEEE754DoubleString(v) {
  2. return toIEEE754Double(v)
  3. .map(function (n) {
  4. for(n = n.toString(2); n.length < 8; n="0"+n);
  5. return n;
  6. })
  7. .join('')
  8. .replace(/(.)(.{11})(.{52})/, "$1 $2 $3")
  9. }
  10.  
  11. function toIEEE754Double(v) {
  12. var ebits = 11, fbits = 52;
  13. var bias = (1 << (ebits - 1)) - 1;
  14. var s, e, f;
  15. if (isNaN(v)) {
  16. e = (1 << bias) - 1; f = Math.pow(2, 51); s = 0;
  17. } else if (v === Infinity || v === -Infinity) {
  18. e = (1 << bias) - 1; f = 0; s = (v < 0) ? 1 : 0;
  19. } else if (v === 0) {
  20. e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0;
  21. } else {
  22. s = v < 0;
  23. v = Math.abs(v);
  24. if (v >= Math.pow(2, 1 - bias)) {
  25. var ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
  26. e = ln + bias;
  27. f = v * Math.pow(2, -ln) * Math.pow(2, fbits) - Math.pow(2, fbits);
  28. } else {
  29. e = 0;
  30. f = v * Math.pow(2, bias - 1) * Math.pow(2, fbits);
  31. }
  32. }
  33. var i, bits = [];
  34. for (i = fbits; i; i -= 1) { bits.push(f % 2 ? 1 : 0); f = Math.floor(f / 2); }
  35. for (i = ebits; i; i -= 1) { bits.push(e % 2 ? 1 : 0); e = Math.floor(e / 2); }
  36. bits.push(s ? 1 : 0);
  37. bits.reverse();
  38. var str = bits.join(''), bytes = [];
  39. while (str.length) {
  40. bytes.push(parseInt(str.substring(0, 8), 2));
  41. str = str.substring(8);
  42. }
  43. return bytes;
  44. }
Add Comment
Please, Sign In to add comment