Merge 6c1fd099806d77342a553d0903009f34f79d15a2 into 4290ea753912378913b1f3f54e0fc5720afeda5d
This commit is contained in:
commit
83313187a7
@ -100,12 +100,15 @@ export function toHexFast(data) {
|
|||||||
* // returns [10,20,30]
|
* // returns [10,20,30]
|
||||||
* fromHex("0a:14:1e", "Colon");
|
* fromHex("0a:14:1e", "Colon");
|
||||||
*/
|
*/
|
||||||
export function fromHex(data, delim="Auto", byteLen=2) {
|
export function fromHex(data, delim="Auto", byteLen=2, removeNonAlphChars=false) {
|
||||||
if (byteLen < 1 || Math.round(byteLen) !== byteLen)
|
if (byteLen < 1 || Math.round(byteLen) !== byteLen)
|
||||||
throw new OperationError("Byte length must be a positive integer");
|
throw new OperationError("Byte length must be a positive integer");
|
||||||
|
|
||||||
|
if (removeNonAlphChars)
|
||||||
|
data = data.replace(/[^\da-fA-F]/g, "");
|
||||||
|
|
||||||
if (delim !== "None") {
|
if (delim !== "None") {
|
||||||
const delimRegex = delim === "Auto" ? /[^a-f\d]|0x/gi : Utils.regexRep(delim);
|
const delimRegex = delim === "Auto" ? /\s+|0x/gi : Utils.regexRep(delim);
|
||||||
data = data.split(delimRegex);
|
data = data.split(delimRegex);
|
||||||
} else {
|
} else {
|
||||||
data = [data];
|
data = [data];
|
||||||
@ -113,6 +116,8 @@ export function fromHex(data, delim="Auto", byteLen=2) {
|
|||||||
|
|
||||||
const output = [];
|
const output = [];
|
||||||
for (let i = 0; i < data.length; i++) {
|
for (let i = 0; i < data.length; i++) {
|
||||||
|
if (/[^a-f\d\s]/.test(data[i]))
|
||||||
|
throw new OperationError("Hex input must only contain hex digits");
|
||||||
for (let j = 0; j < data[i].length; j += byteLen) {
|
for (let j = 0; j < data[i].length; j += byteLen) {
|
||||||
output.push(parseInt(data[i].substr(j, byteLen), 16));
|
output.push(parseInt(data[i].substr(j, byteLen), 16));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,6 +30,11 @@ class FromHex extends Operation {
|
|||||||
name: "Delimiter",
|
name: "Delimiter",
|
||||||
type: "option",
|
type: "option",
|
||||||
value: FROM_HEX_DELIM_OPTIONS
|
value: FROM_HEX_DELIM_OPTIONS
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Remove non-alphabet chars",
|
||||||
|
type: "boolean",
|
||||||
|
value: true
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
this.checks = [
|
this.checks = [
|
||||||
@ -92,8 +97,8 @@ class FromHex extends Operation {
|
|||||||
* @returns {byteArray}
|
* @returns {byteArray}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const delim = args[0] || "Auto";
|
const [delim, removeNonAlphChars] = [args[0] || "Auto", args[1]];
|
||||||
return fromHex(input, delim, 2);
|
return fromHex(input, delim, 2, removeNonAlphChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user