Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,17 @@ <h2>64-bit (double)</h2>

var tmpBytes = toByteArray(hex.value);
bytes.fill(0);
bytes.set(tmpBytes.reverse(), hexLength - tmpBytes.length);
// tmpBytes is in big-endian order (MSB first from hex string)
// bytes array is in little-endian order (LSB at index 0)
// So we reverse tmpBytes and place from the end
tmpBytes.reverse();
for (var i = 0; i < tmpBytes.length; i++) {
bytes[i] = tmpBytes[i];
}
render();
};
hex.onblur = function () {
render();
// Don't reformat the hex value on blur - keep what the user typed
};

input.onmousedown = function (e) {
Expand Down Expand Up @@ -426,15 +432,17 @@ <h2>64-bit (double)</h2>
}

function toHexString(byteArray) {
return Array.from(byteArray, function (byte) {
var hexStr = Array.from(byteArray, function (byte) {
return ('0' + byte.toString(16).toUpperCase()).slice(-2);
}).join('')
}).join('');
// Remove leading zeros but keep at least one digit
return hexStr.replace(/^0+(?=.)/, '') || '0';
}

function toByteArray(hexString) {
var result = [];
if (hexString.length % 2 == 1) {
hexString = hexString + '0';
hexString = '0' + hexString;
}
for (var i = 0; i < hexString.length; i += 2) {
result.push(parseInt(hexString.substr(i, 2), 16));
Expand Down