2017-04-15 19:04:49 +02:00
|
|
|
import {Converter} from './converter';
|
2017-03-14 15:42:11 +01:00
|
|
|
|
2016-09-20 21:12:13 +02:00
|
|
|
export class HexToDecConverter implements Converter {
|
2017-04-15 18:51:22 +02:00
|
|
|
getDisplayname(): string {
|
2017-04-15 19:04:49 +02:00
|
|
|
return 'Convert hexadecimal to decimal';
|
2017-04-15 18:51:22 +02:00
|
|
|
}
|
2016-09-20 21:12:13 +02:00
|
|
|
|
2017-04-15 18:51:22 +02:00
|
|
|
getId(): string {
|
2017-04-15 19:04:49 +02:00
|
|
|
return 'hextodec';
|
2017-04-15 18:51:22 +02:00
|
|
|
}
|
2016-09-20 21:12:13 +02:00
|
|
|
|
2017-04-15 18:51:22 +02:00
|
|
|
convert(input: string): string {
|
2017-04-15 19:07:34 +02:00
|
|
|
const n: number = parseInt(input, 16);
|
2018-08-31 23:59:52 +02:00
|
|
|
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.');
|
2016-09-20 21:12:13 +02:00
|
|
|
}
|
2017-04-15 18:51:22 +02:00
|
|
|
return n.toString(10);
|
|
|
|
}
|
2016-09-20 21:12:13 +02:00
|
|
|
}
|