converter/src/app/converter/hex-to-dec-converter.ts

20 lines
489 B
TypeScript

import {Converter} from './converter';
export class HexToDecConverter implements Converter {
getDisplayname(): string {
return 'Convert hexadecimal to decimal';
}
getId(): string {
return 'hextodec';
}
convert(input: string): string {
const n: number = parseInt(input, 16);
if (isNaN(n) || !input.trim().match(/^((0x|0X)?[0-9a-fA-F]+)$/)) {
throw new Error('The input seems not to be a valid hexadecimal number.');
}
return n.toString(10);
}
}