都是 int8 呗?那直接位移更简洁一些:
function hex2dec_int8(hex) {
const BITS = 8;
const dec = parseInt(hex, 16);
return 0x80000000 >> (32 - BITS) | dec;
}
console.log(hex2dec_int8('c4'));
可以直接简化成:
const hex2dec_int8 = (hex) => -128 | parseInt(hex, 16);
console.log(hex2dec_int8('c4'));
应该不会有比这个更简便更高效的写法了。