Search code examples
javascriptparseint

javascript: convert hexadecimal value into signed 8-bit integer


I need a way of getting a signed 8-bit integer from a hexadecimal value using JavaScript. So far I have tried using parseInt(value, 8) but it seems to be deprecated and I get parseInt(0xbd, 8) = 0 (when it's supposed to give -67).

How can I do this?


Solution

  • I was just searching for a solution to this in javascript. The answer from splig does not seem correct, but it lead me to the solution.

    I believe you should be subtracting 256 from num when (num > 127).

    var num = parseInt('ff', 16);
    if (num > 127) { num = num - 256 }
    alert(num);