Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented new hex tranformer #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 13 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(),
Expand All @@ -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);
}
});
Expand Down
43 changes: 28 additions & 15 deletions src/lib/hex.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
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 {
return this.label;
}

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;
}
}

37 changes: 37 additions & 0 deletions src/lib/jsonhex.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}