/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ 'use strict'; export class Converter{ // Decimal to HexaDecimal conversion public static dec2hex(val :number) :string{ return (+val).toString(16).toUpperCase(); } // Decimal to Binary conversion public static dec2bin(val :number) :string{ return (+val).toString(2); } // Decimal to Ascii conversion public static dec2asc(val :number) :string{ if (val > 127){ throw Error('Value out of range'); } else { return String.fromCharCode(+val); } } // Decimal to Unicode conversion public static dec2uni(val :number) :string{ return String.fromCharCode(val); } // Decimal to AsciiString conversion public static dec2ascStr(val :number) :string{ return this.hex2asc(this.dec2hex(val)); } // Decimal to UnicodeString conversion public static dec2uniStr(val :number) :string{ return this.hex2uniStr(this.dec2hex(val)); } // HexaDecimal to Decimal conversion public static hex2dec(val :string) :number{ return parseInt(val, 16); } // HexaDecimal to Binary conversion public static hex2bin(val :string) :string{ return this.hex2dec(val).toString(2); } // HexaDecimal to Ascii hex conversion public static hex2asc(val :string) :string{ let tempstr = ''; for (let a = 0; a < val.length; a += 2) { let charval = parseInt(val.substr(a, 2), 16) if(charval < 127){ tempstr = tempstr + String.fromCharCode(charval); } else { throw Error('Value at '+a+' out of range'); } } return tempstr; } //HexaDecimal to Unicode Char conversion public static hex2uni(val :string) :string{ return String.fromCharCode(parseInt(val, 16)); } //HexaDecimal to Unicode String conversion public static hex2uniStr(val :string) :string{ let tempstr = ''; for (let a = 0; a < val.length; a += 2) { tempstr = tempstr + this.hex2uni(val.substr(a, 2)); } return tempstr; } // Binary to Decimal conversion public static bin2dec(val :string) :number{ return parseInt(val, 2); } // Binary to HexaDecimal conversion public static bin2hex(val :string) :string{ return parseInt(val, 2).toString(16).toUpperCase(); } // Binary to Ascii conversion public static bin2asc(val :string) :string{ return this.hex2asc(this.bin2hex(val)); } //Binary to Unicode String conversion public static bin2uni(val :string) :string{ return this.hex2uni(this.bin2hex(val)); } // Binary to Unicode String conversion public static bin2uniStr(val :string) :string{ return this.hex2uniStr(this.bin2hex(val)); } // Ascii to HexaDecimal conversion public static asc2hex(val :string) :string{ let hexstr = ''; for (let a = 0; a < val.length; a++) { let cCode = val.charCodeAt(a); if (cCode > 127){ throw Error('out of range: non-ASCII character found') } else { hexstr = hexstr + cCode.toString(16).toUpperCase(); } } return hexstr; } // Ascii to Decimal conversion public static asc2dec(val :string) :number{ return this.hex2dec(this.asc2hex(val)); } // Ascii to Binary conversion public static asc2bin(val :string) :string{ return this.hex2bin(this.asc2hex(val)); } // AsciiString to Decimal conversion public static ascStr2dec(val :string) :number{ return this.hex2dec(this.asc2hex(val)); } //Unicode to HexaDecimal conversion public static uni2hex(val :string) :string{ let hexstr = ''; for (let a = 0; a < val.length; a++) { hexstr = hexstr + val.charCodeAt(a).toString(16).toUpperCase(); } return hexstr; } //Unicode to Decimal conversion public static uni2dec(val :string) :number{ return this.hex2dec(this.uni2hex(val)); } // convert 0..255 R,G,B values to binary string public static RGB2Bin(r :number, g :number, b :number) :string{ let bin = r << 16 | g << 8 | b; return (function (h) { return new Array(25 - h.length).join("0") + h })(bin.toString(2)) } // convert 0..255 R,G,B values to a hexidecimal color string public static RGB2Hex(r :number, g :number, b:number) :string{ let bin = r << 16 | g << 8 | b; return (function (h) { return new Array(7 - h.length).join("0") + h })(bin.toString(16).toUpperCase()) } // convert a 24 bit binary color to 0..255 R,G,B public static bin2RGB(bin :string) :number[]{ let pbin = parseInt(bin, 2); let r = pbin >> 16; let g = pbin >> 8 & 0xFF; let b = pbin & 0xFF; return [r, g, b]; } // convert a hexidecimal color string to 0..255 R,G,B public static hex2RGB(hex :string) :number[]{ let rgb = parseInt(hex, 16); let r = rgb >> 16; let g = rgb >> 8 & 0xFF; let b = rgb & 0xFF; return [r, g, b]; } }