JavaScript 数据处理 - 处理十六进制数字字符串(十六进制数字字符串校验、十六进制数字字符串消除开头的 0)
·
一、十六进制数字字符串校验
实例实操 1
(1)需求
-
判断一个十六进制数字字符串是否是有效的一个十六进制数
-
前缀有无 0x 均可
-
范围是 0x0 ~ 0xffffffffffff
-
不可以是负数
(2)具体实现
- Code
function isValidHexString(str) {
// 匹配十六进制格式
const hexRegex = /^(0x)?[0-9a-fA-F]+$/;
if (!hexRegex.test(str)) return false;
const cleanHex = str.startsWith("0x") ? str.slice(2) : str;
try {
const value = BigInt("0x" + cleanHex);
return value >= 0 && value <= 0xffffffffffffn;
} catch (e) {
return false;
}
}
- Test
console.log(isValidHexString("-0x1"));
console.log(isValidHexString("-0xffffffffffff"));
console.log(isValidHexString("0xffffffffffff"));
console.log(isValidHexString("-0x1000000000000"));
console.log(isValidHexString("-1a"));
# 输出结果
false
false
true
false
false
console.log(isValidHexString("0x1"));
console.log(isValidHexString("abc"));
console.log(isValidHexString("-abc"));
console.log(isValidHexString("--0x1"));
console.log(isValidHexString("-0xhello"));
# 输出结果
true
true
false
false
false
console.log(isValidHexString(""));
# 输出结果
false
实例实操 2
(1)需求
-
判断一个十六进制数字字符串是否是有效的一个十六进制数
-
前缀有无 0x 均可
-
范围是 min ~ max
-
可以是负数
(2)具体实现
- Code
function isValidHexString(str, min = 0n, max = 0xffffffffffffn) {
if (typeof min !== "bigint" || typeof max !== "bigint") {
throw new Error("min and max must be bigint");
}
if (min > max) {
throw new Error("min cannot be greater than max");
}
// 匹配十六进制格式
const hexRegex = /^(-)?(0x)?[0-9a-fA-F]+$/;
if (!hexRegex.test(str)) return false;
const isNegative = str.startsWith("-");
let cleanHex = str;
if (isNegative) cleanHex = cleanHex.slice(1);
if (cleanHex.startsWith("0x")) cleanHex = cleanHex.slice(2);
try {
const value = BigInt("0x" + cleanHex);
const finalValue = isNegative ? -value : value;
return finalValue >= min && finalValue <= max;
} catch (e) {
return false;
}
}
- Test
console.log("默认范围测试");
console.log(isValidHexString("0x1"));
console.log(isValidHexString("-0x1"));
console.log(isValidHexString("-0xffffffffffff"));
console.log(isValidHexString("0xffffffffffff"));
console.log(isValidHexString("-0x1000000000000"));
console.log(isValidHexString("0x1000000000000"));
# 输出结果
默认范围测试
true
false
false
true
false
false
console.log("自定义范围测试");
console.log(isValidHexString("0x7fff", -0x8000n, 0x7fffn));
console.log(isValidHexString("-0x8000", -0x8000n, 0x7fffn));
console.log(isValidHexString("-0x8001", -0x8000n, 0x7fffn));
console.log(isValidHexString("0x8000", -0x8000n, 0x7fffn));
# 输出结果
自定义范围测试
true
true
false
false
console.log("负数范围测试");
console.log(isValidHexString("-0x100", -0x1000n, -0x100n));
console.log(isValidHexString("-0x50", -0x1000n, -0x100n));
console.log(isValidHexString("-0x2000", -0x1000n, -0x100n));
# 输出结果
负数范围测试
true
false
false
console.log("多种格式测试");
console.log(isValidHexString("-1a"));
console.log(isValidHexString("-0xFF"));
console.log(isValidHexString("abc"));
console.log(isValidHexString("-abc"));
# 输出结果
多种格式测试
false
false
true
false
console.log("错误格式测试");
console.log(isValidHexString("--0x1"));
console.log(isValidHexString("-0xhello"));
console.log(isValidHexString(""));
# 输出结果
错误格式测试
false
false
false
console.log("错误参数测试");
try {
isValidHexString("0x1", "0", "100");
} catch (e) {
console.log("参数错误:", e.message);
}
try {
isValidHexString("0x1", 100n, 0n);
} catch (e) {
console.log("范围错误:", e.message);
}
# 输出结果
错误参数测试
参数错误: min and max must be bigint
范围错误: min cannot be greater than max
实例实操 3
(1)需求
-
判断一个十六进制数字字符串是否是有效的一个十六进制数
-
前缀无 0x
-
范围是 min ~ max
-
不可以是负数
(2)具体实现
- Code
function isValidHexString(str, min = 0n, max = 0xffffffffffffn) {
if (typeof min !== "bigint" || typeof max !== "bigint") {
throw new Error("min and max must be bigint");
}
if (min > max) {
throw new Error("min cannot be greater than max");
}
const hexRegex = /^[0-9a-fA-F]+$/;
if (!hexRegex.test(str)) return false;
try {
const value = BigInt("0x" + str);
return value >= min && value <= max;
} catch (e) {
return false;
}
}
- Test
console.log(isValidHexString("-0x1"));
console.log(isValidHexString("-0xffffffffffff"));
console.log(isValidHexString("0xffffffffffff"));
console.log(isValidHexString("-0x1000000000000"));
console.log(isValidHexString("-1a"));
# 输出结果
false
false
false
false
false
console.log(isValidHexString("0x1"));
console.log(isValidHexString("abc"));
console.log(isValidHexString("-abc"));
console.log(isValidHexString("--0x1"));
console.log(isValidHexString("-0xhello"));
# 输出结果
false
true
false
false
false
console.log(isValidHexString(""));
# 输出结果
false
二、十六进制数字字符串消除开头的 0
1、使用 parseInt 函数
- Code
// 先将字符串转为数字,再转回十六进制字符串
function removeHexLeadingZeros(hexStr) {
const num = parseInt(hexStr, 16);
return num.toString(16).toUpperCase();
}
- Test
console.log(removeHexLeadingZeros("000ABC"));
console.log(removeHexLeadingZeros("0x000123"));
console.log(removeHexLeadingZeros("0000"));
# 输出结果
ABC
123
0
2、使用 parseInt 函数(保留 0x 前缀)
- Code
function removeHexLeadingZeros(hexStr) {
const hasPrefix = hexStr.toLowerCase().startsWith("0x");
let cleanStr = hexStr;
if (hasPrefix) cleanStr = hexStr.substring(2);
const num = parseInt(cleanStr, 16);
const result = num.toString(16).toUpperCase();
if (hasPrefix) return "0x" + result;
return result;
}
- Test
console.log(removeHexLeadingZeros("000ABC"));
console.log(removeHexLeadingZeros("0x000123"));
console.log(removeHexLeadingZeros("0000"));
console.log(removeHexLeadingZeros("0x0000"));
# 输出结果
ABC
0x123
0
0x0
3、使用正则表达式
- Code
function removeHexLeadingZeros(hexStr) {
// 处理带有 0x 前缀的情况
if (/^0x/i.test(hexStr)) {
const valuePart = hexStr.substring(2);
const cleaned = valuePart.replace(/^0+(?=[0-9A-Fa-f])/, "") || "0";
return "0x" + cleaned;
}
// 处理不带 0x 前缀的情况
return hexStr.replace(/^0+(?=[0-9A-Fa-f])/, "") || "0";
}
- Test
console.log(removeHexLeadingZeros("000ABC"));
console.log(removeHexLeadingZeros("0x000123"));
console.log(removeHexLeadingZeros("0000"));
# 输出结果
ABC
0x123
0
三、补充学习
1、十进制数字字符串消除开头的 0
(1)使用 parseInt 函数
- Code
function removeLeadingZeros(str) {
return parseInt(str).toString();
}
- Test
console.log(removeLeadingZeros("000123"));
console.log(removeLeadingZeros("0000"));
console.log(removeLeadingZeros("001.23"));
# 输出结果
123
0
1
(2)使用 parseFloat 函数
- Code
function removeLeadingZeros(str) {
return parseFloat(str).toString();
}
- Test
console.log(removeLeadingZeros("000123"));
console.log(removeLeadingZeros("0000"));
console.log(removeLeadingZeros("001.23"));
# 输出结果
123
0
1.23
(3)使用正则表达式
- Code
function removeLeadingZeros(str) {
return str.replace(/^0+(?=\d)/, "");
}
- Test
console.log(removeLeadingZeros("000123"));
console.log(removeLeadingZeros("0000"));
console.log(removeLeadingZeros("001.23"));
# 输出结果
123
0
1.23
2、关于 parseInt 函数
- parseInt 函数用于将字符串解析为整数,它可以接收有前缀的十六进制字符串,也可以接收没有前缀的十六进制字符串
const result1 = parseInt("ff", 16);
const result2 = parseInt("0xff", 16);
console.log(result1);
console.log(result2);
# 输出结果
255
255
3、bigint 字面量
- bigint 字面量是一种用于表示大整数的语法,它以 n 结尾
const num1 = 0;
const num2 = 0xffffffffffff
const bigNum1 = 0n;
const bigNum2 = 0xffffffffffffn;
console.log(typeof num1);
console.log(typeof num2);
console.log(typeof bigNum1);
console.log(typeof bigNum2);
# 输出结果
number
number
bigint
bigint
更多推荐

所有评论(0)