JS code do conversion between hexStr and byteArray
2015-05-20 00:51
381 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Online Cryptographic Calculator</title>
</head>
<input style="width:80%" id="input" value="12-AB">
<input type="button" value="Calculate" onclick="output.value=buttonHandler(input.value)">
<textarea style="width:80%" rows=10 id=output></textarea>
<script>
/* Convert a hex char to value */
function hexChar2byte(c)
{
var d = 0;
if (c>='A' && c<='F')
{
d = c.charCodeAt(0) - 'A'.charCodeAt(0) + 10;
}
else if (c>='a' && c<='f')
{
d = c.charCodeAt(0) - 'a'.charCodeAt(0) + 10;
}
else if (c>='0' && c<='9')
{
d = c.charCodeAt(0) - '0'.charCodeAt(0);
}
return d;
}
/* Check if a char is hex char */
function isHexChar(c)
{
if ((c>='A' && c<='F') ||
(c>='a' && c<='a') ||
(c>='0' && c<='9'))
{
return 1;
}
return 0;
}
/* Convert HEX string to byte array */
function hexStr2byteArray(str)
{
var byteArray = Array();
var d = 0;
var i = 0;
var j = 0;
var k = 0;
for (i=0; i<str.length; i++)
{
var c = str.charAt(i);
if (isHexChar(c))
{
d <<= 4;
d += hexChar2byte(c);
j++;
if (0==(j%2))
{
byteArray[k++] = d;
d = 0;
}
}
}
return byteArray;
}
/* Convert a byte to string */
function byte2hexStr(byte)
{
var hexByteMap = "0123456789ABCDEF";
var str = "";
str += hexByteMap.charAt(byte >> 4);
str += hexByteMap.charAt(byte & 0x0f);
return str;
}
/* Convert byte arry to HEX string */
function byteArray2hexStr(byteArray)
{
var str = "";
for (var i=0; i<(byteArray.length-1); i++)
{
str += byte2hexStr(byteArray[i]);
str += "-";
}
str += byte2hexStr(byteArray[i]);
return str;
}
/* Button handler */
function buttonHandler(str)
{
return byteArray2hexStr(hexStr2byteArray(str));
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Online Cryptographic Calculator</title>
</head>
<input style="width:80%" id="input" value="12-AB">
<input type="button" value="Calculate" onclick="output.value=buttonHandler(input.value)">
<textarea style="width:80%" rows=10 id=output></textarea>
<script>
/* Convert a hex char to value */
function hexChar2byte(c)
{
var d = 0;
if (c>='A' && c<='F')
{
d = c.charCodeAt(0) - 'A'.charCodeAt(0) + 10;
}
else if (c>='a' && c<='f')
{
d = c.charCodeAt(0) - 'a'.charCodeAt(0) + 10;
}
else if (c>='0' && c<='9')
{
d = c.charCodeAt(0) - '0'.charCodeAt(0);
}
return d;
}
/* Check if a char is hex char */
function isHexChar(c)
{
if ((c>='A' && c<='F') ||
(c>='a' && c<='a') ||
(c>='0' && c<='9'))
{
return 1;
}
return 0;
}
/* Convert HEX string to byte array */
function hexStr2byteArray(str)
{
var byteArray = Array();
var d = 0;
var i = 0;
var j = 0;
var k = 0;
for (i=0; i<str.length; i++)
{
var c = str.charAt(i);
if (isHexChar(c))
{
d <<= 4;
d += hexChar2byte(c);
j++;
if (0==(j%2))
{
byteArray[k++] = d;
d = 0;
}
}
}
return byteArray;
}
/* Convert a byte to string */
function byte2hexStr(byte)
{
var hexByteMap = "0123456789ABCDEF";
var str = "";
str += hexByteMap.charAt(byte >> 4);
str += hexByteMap.charAt(byte & 0x0f);
return str;
}
/* Convert byte arry to HEX string */
function byteArray2hexStr(byteArray)
{
var str = "";
for (var i=0; i<(byteArray.length-1); i++)
{
str += byte2hexStr(byteArray[i]);
str += "-";
}
str += byte2hexStr(byteArray[i]);
return str;
}
/* Button handler */
function buttonHandler(str)
{
return byteArray2hexStr(hexStr2byteArray(str));
}
</script>
相关文章推荐
- Go conversion between struct and byte array
- BYTE TO HEX AND HEX TO BYTE STRING CONVERSION
- Go conversion between struct and byte
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- How to find the relation between role and user, t-code, profile
- What is the difference between Array.Clone and Array.Copy Method
- Transform between Hex and Dec in Javascript
- Codeforces Round #179 (Div. 1) A. Greg and Array
- The difference between pointer and array
- Convert a string to a byte array and vica-versa in C#
- How to make conversion between RGB and HSV? [Python sourcecode]
- java hex binary data to byte array
- Do you know the difference between dimensional modeling truth and fiction?
- Lesha and array splitting CodeForces - 754A
- AndBug调试Android Dalvik Bytecode
- The differences between AngularJS $apply, $digest, and $evalAsync
- Conversion between json and object
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Code:关于加密解密 Base64 and URL and Hex Encoding and Decoding
- LintCode on Array by Odd and Even