diff --git a/src/extension.ts b/src/extension.ts index 8b1a387..3c36d60 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -10,6 +10,7 @@ import * as unicode from './lib/unicode'; import * as urlEncode from './lib/urlencode'; import * as xmlentities from './lib/xmlentities'; import * as yaml from './lib/yaml'; +import * as jsonhex from './lib/jsonhex'; import * as hex from './lib/hex'; class Context { @@ -61,11 +62,10 @@ class Change { } public updateSelection() { - if (this.updatedSelectionStartOffset != undefined && this.output != undefined) - { + if (this.updatedSelectionStartOffset != undefined && this.output != undefined) { let updatedSelectionStart = this.textEditor.document.positionAt(this.updatedSelectionStartOffset); let updatedSelectionEnd = this.textEditor.document.positionAt(this.updatedSelectionStartOffset + this.output.length); - + // Build and store the new selection. this.updatedSelection = new vscode.Selection(updatedSelectionStart, updatedSelectionEnd); } @@ -124,6 +124,8 @@ function selectAndApplyTransformation(textEditor: vscode.TextEditor, edit: vscod let transformers: core.Transformer[] = [ new base64.StringToBase64Transformer(), new base64.Base64ToStringTransformer(), + new hex.StringToHexTransformer(), + new hex.HexToStringTransformer(), new json.StringToJsonArrayTransformer(), new json.StringToJsonStringTransformer(), new json.JsonStringToStringTransformer(), @@ -143,18 +145,17 @@ function selectAndApplyTransformation(textEditor: vscode.TextEditor, edit: vscod new xmlentities.XmlEntitiesToStringTransformer(), new crockford32.IntegerToCrockfordBase32Transformer(), new crockford32.CrockfordBase32ToIntegerTransformer(), - new unicode.StringToUnicodeTransformer(), - new unicode.UnicodeToStringTransformer(), - new urlEncode.StringToEncodedUrlTransformer(), - new urlEncode.EncodedUrlToStringTransformer(), - new yaml.JsonToYamlTransformer(), - new yaml.YamlToJsonTransformer(), - new hex.JsonByteArrayToHexStringTransformer() + new unicode.StringToUnicodeTransformer(), + new unicode.UnicodeToStringTransformer(), + new urlEncode.StringToEncodedUrlTransformer(), + new urlEncode.EncodedUrlToStringTransformer(), + new yaml.JsonToYamlTransformer(), + new yaml.YamlToJsonTransformer(), + new jsonhex.JsonByteArrayToHexStringTransformer() ]; vscode.window.showQuickPick(transformers).then((transformer) => { - if (transformer != undefined) - { + if (transformer != undefined) { processSelections(textEditor, edit, transformer); } }); diff --git a/src/lib/hex.ts b/src/lib/hex.ts index fa69266..bac1090 100644 --- a/src/lib/hex.ts +++ b/src/lib/hex.ts @@ -1,8 +1,8 @@ import * as core from './core'; -export class JsonByteArrayToHexStringTransformer implements core.Transformer { +export class StringToHexTransformer implements core.Transformer { public get label(): string { - return 'JSON Byte Array to Hex String'; + return 'String to Hex'; } public get description(): string { @@ -10,28 +10,41 @@ export class JsonByteArrayToHexStringTransformer implements core.Transformer { } public check(input: string): boolean { - let parsedInput = JSON.parse(input); - if (!Array.isArray(parsedInput)) return false; + return true; + } - for (let i = 0; i < parsedInput.length; i++) { - let element = parsedInput[i]; - if (!Number.isInteger(element)) return false; - if (element < 0 || element > 255) return false; + public transform(input: string): string { + let output = ""; + for (let i = 0; i < input.length; i++) { + const charCode = input.charCodeAt(i); + const hex = charCode.toString(16); + output += hex.padStart(2, "0"); } + return output; + } +} + +export class HexToStringTransformer implements core.Transformer { + public get label(): string { + return "Hex to String"; + } + public get description(): string { + return this.label; + } + + public check(input: string): boolean { return true; } public transform(input: string): string { - let parsedInput = JSON.parse(input); let output = ''; - - for (let i = 0; i < parsedInput.length; i++) { - let element = parsedInput[i]; - let hexElement = element.toString(16).padStart(2, '0'); - output += hexElement; + for (let i = 0; i < input.length; i += 2) { + const hex = input.substring(i, i + 2); + const charCode = parseInt(hex, 16); + output += String.fromCharCode(charCode); } - return output; } } + diff --git a/src/lib/jsonhex.ts b/src/lib/jsonhex.ts new file mode 100644 index 0000000..fa69266 --- /dev/null +++ b/src/lib/jsonhex.ts @@ -0,0 +1,37 @@ +import * as core from './core'; + +export class JsonByteArrayToHexStringTransformer implements core.Transformer { + public get label(): string { + return 'JSON Byte Array to Hex String'; + } + + public get description(): string { + return this.label; + } + + public check(input: string): boolean { + let parsedInput = JSON.parse(input); + if (!Array.isArray(parsedInput)) return false; + + for (let i = 0; i < parsedInput.length; i++) { + let element = parsedInput[i]; + if (!Number.isInteger(element)) return false; + if (element < 0 || element > 255) return false; + } + + return true; + } + + public transform(input: string): string { + let parsedInput = JSON.parse(input); + let output = ''; + + for (let i = 0; i < parsedInput.length; i++) { + let element = parsedInput[i]; + let hexElement = element.toString(16).padStart(2, '0'); + output += hexElement; + } + + return output; + } +}