netkitty
Version:
Network tools kit
40 lines (39 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HexToFloat32 = exports.HexToUInt64 = exports.HexToUInt32 = exports.HexToUInt16 = exports.HexToUInt8 = exports.HexToInt64 = exports.HexToInt32 = exports.HexToInt16 = exports.HexToInt8 = void 0;
const HexToInt8 = (hex) => Int8Array.from([parseInt(hex, 16)])[0];
exports.HexToInt8 = HexToInt8;
const HexToInt16 = (hex) => Int16Array.from([parseInt(hex, 16)])[0];
exports.HexToInt16 = HexToInt16;
const HexToInt32 = (hex) => Int32Array.from([parseInt(hex, 16)])[0];
exports.HexToInt32 = HexToInt32;
const HexToInt64 = (hex) => {
const INT64_MAX = BigInt('9223372036854775807');
const INT64_MIN = BigInt('-9223372036854775808');
const bigIntValue = BigInt(`0x${hex}`);
if (bigIntValue < INT64_MIN || bigIntValue > INT64_MAX)
return INT64_MAX;
return bigIntValue;
};
exports.HexToInt64 = HexToInt64;
const HexToUInt8 = (hex) => Uint8Array.from([parseInt(hex, 16)])[0];
exports.HexToUInt8 = HexToUInt8;
const HexToUInt16 = (hex) => Uint16Array.from([parseInt(hex, 16)])[0];
exports.HexToUInt16 = HexToUInt16;
const HexToUInt32 = (hex) => Uint32Array.from([parseInt(hex, 16)])[0];
exports.HexToUInt32 = HexToUInt32;
const HexToUInt64 = (hex) => {
const INT64_MAX = BigInt('18446744073709551615');
const bigIntValue = BigInt(`0x${hex}`);
if (bigIntValue < 0n || bigIntValue > INT64_MAX)
return INT64_MAX;
return bigIntValue;
};
exports.HexToUInt64 = HexToUInt64;
const HexToFloat32 = (hex) => {
const buffer = new ArrayBuffer(4);
const dataView = new DataView(buffer);
dataView.setUint32(0, parseInt(hex, 16), false);
return dataView.getFloat32(0, false);
};
exports.HexToFloat32 = HexToFloat32;