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

20 lines
489 B
TypeScript
Raw Normal View History

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