26 lines
563 B
TypeScript
Raw Normal View History

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'hex2ascii'
})
export class Hex2asciiPipe implements PipeTransform {
transform(hex: string): string {
const opPush = hex.split(' ').filter((_, i, a) => i > 0 && /^OP_PUSH/.test(a[i - 1]));
if (opPush[0]) {
hex = opPush[0];
}
if (!hex) {
return '';
}
2021-03-03 21:29:22 +09:00
let bytes: number[] = [];
for (let i = 0; i < hex.length; i += 2) {
2021-03-03 21:29:22 +09:00
bytes.push(parseInt(hex.substr(i, 2), 16));
}
2021-03-03 21:29:22 +09:00
return new TextDecoder('utf8').decode(Uint8Array.from(bytes));
}
}