之前写的系统信息收集报告程序SysInfo的一个功能就是收集并报告系统中的硬盘序列号。在之前的测试中这项功能表现不错,但前两天用SysInfo收集一台电脑的信息时,显示的硬盘序列号与其它硬盘序列号读取程序显示的顺序不一样。于是着手对SysInfo的相关代码进行修改,由于读取到的硬盘序列号原始数据是一个16进制字符串,比如“39574d41374d5659”,我们首先要把这个16进制字符串转换成对应的ASCII,才好进一步做对比分析。
在这方面,直接用JavaScript应该是最方便的了。因为JavaScript提供了两个非常有用的函数:parseInt()和String.fromCharCode()。
一、parseInt()
首先说说parseInt()。
(一)功能:解析字符串并返回整数。
(二)语法:
parseInt(string, radix)
(三)参数说明:
(四)返回值:
二、String.fromCharCode()
(一)功能:可接受一个指定的 Unicode 值,然后返回一个字符串。
(二)语法:
String.fromCharCode(numX,numX,...,numX)
(三)参数说明:
(四)返回值:
为了方便使用,我用editplus设计了一web页面,在一个form中包括了四个部分:
一是输入待转换字符串的文本框;
二是选择待转换字符串的进制基数的radio,有2、8、10、16四个常用的选项,其中默认选定16进制;
三是输出转换后的ASCII的文本框;
四是实现转换功能的按钮。
代码如下:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="Generator" content="EditPlus®"><meta name="Author" content=""><meta name="Keywords" content=""><meta name="Description" content=""><title>String 2 ASCII</title></head><body><form id="frmScale" style="border:3px double black; padding:10px; width:600px;"><P style="text-align:center;font-size:18pt; color:purple;">String 2 ASCII</P><p>待转换代码:<input type="text" name="txtHex" value="39574d41374d5659" id="txtHex" size="50"></p><p>代码进制基数:<input type="radio" name="scale" value="2">2进制 <input type="radio" name="scale" value="8">8进制 <input type="radio" name="scale" value="10">10进制 <input type="radio" name="scale" value="16" checked="checked">16进制</p><p>ASCII :<input type="text" name="txtASCII" id="txtASCII" size="50"></p><p style="margin-left:90%;"><input type="button" value="转换" onclick="Hex2ASCII()"></p></form></body></html>
配套的JavaScrpit转换代码如下:
<script>function Hex2ASCII(){var tHex = document.getElementById("txtHex").value;var iHexlen = tHex.length;if (0==iHexlen){alert("请先输入代码!");}else{var iScale = document.getElementById("frmScale").scale.value;if (null==iScale){alert("请先选择待转换字符串的进制基数!");}else{//alert(iScale);var r = "";for (i=0; i <=iHexlen; i=i+2){//alert(parseInt(tHex.substring(i,i+2),16) + "<BR>" + String.fromCharCode(parseInt(tHex.substring(i,i+2),16)));r += String.fromCharCode(parseInt(tHex.substring(i,i+2),iScale));//alert(r);}document.getElementById("txtASCII").value = r;}}}</script>
这段JavaScript代码在Edge浏览器中运行正常,但直接在EditPlus中运行时,即使在html代码中已经默认选定16进制,但
var iScale = document.getElementById("frmScale").scale.value;
返回值仍为null:
为了保证通用性,增加了一个GetRadioValue()函数,完整代码如下:
<script>function GetRadioValue(RadioName){var rb, v = null; rb = document.getElementsByName(RadioName);if (null != rb){var i;for(i=0; i<rb.length; i++){if(rb[i].checked){v = rb[i].value; }}}return v;} function Hex2ASCII(){var tHex = document.getElementById("txtHex").value;var iHexlen = tHex.length;if (0==iHexlen){alert("请先输入代码!");}else{//var iScale = document.getElementById("frmScale").scale.value;//在Edge浏览器中此句可用,但在editplus中无效var iScale = GetRadioValue("scale");if (null==iScale){alert("请先选择待转换字符串的进制基数!");}else{alert(iScale);r = "";for (i=0; i <=iHexlen; i=i+2){alert(parseInt(tHex.substring(i,i+2),16) + "<BR>" + String.fromCharCode(parseInt(tHex.substring(i,i+2),16)));r += String.fromCharCode(parseInt(tHex.substring(i,i+2),iScale));//alert(r);}document.getElementById("txtASCII").value = r;}}}</script>
用JavaScript写代码将硬盘序列号从16进制字符串转换为ASCII字符串 兼谈EditPlus和Edge浏览器对JavaScript脚本支持的一点差别