Skip to content

Instantly share code, notes, and snippets.

@RedDragon55
Created February 22, 2019 15:04
Show Gist options
  • Select an option

  • Save RedDragon55/00bb3b54d468da70de957f30dc16f379 to your computer and use it in GitHub Desktop.

Select an option

Save RedDragon55/00bb3b54d468da70de957f30dc16f379 to your computer and use it in GitHub Desktop.

Revisions

  1. RedDragon55 created this gist Feb 22, 2019.
    23 changes: 23 additions & 0 deletions toInt8.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    function ToInteger(x) {
    x = Number(x);
    return x < 0 ? Math.ceil(x) : Math.floor(x);
    }

    function modulo(a, b) {
    return a - Math.floor(a/b)*b;
    }

    function ToUint8(x) {
    return modulo(ToInteger(x), Math.pow(2, 8));
    }

    function ToInt8(x) {
    var uint8 = ToUint8(x);
    if (uint8 >= Math.pow(2, 7)) {
    return uint8 - Math.pow(2, 8)
    } else {
    return uint8;
    }
    }

    console.log(ToInt8(parseInt("80", 16)))