Conv-Decimal to Hex
From Vectorlab
Converts a decimal value in range 0-255 (8-bit) into it's hexadecimal equivalent. Returns a string. There are two variants, one padding with zero, the other adding the pound sign (good for hex colors).
{ Orso ************************************************ } { convert integer in range 0-255 to hex, padding with "0" } { for example: } { U_Dec2Hex(255) --> FF } { U_Dec2Hex(0) --> 00 } { U_Dec2Hex(126) --> 7E } FUNCTION U_Dec2Hex(decVal: INTEGER): STRING; VAR rem : INTEGER; hex : STRING; seq : DYNARRAY[] OF CHAR; BEGIN seq := '0123456789ABCDEF'; hex := ''; REPEAT rem := decVal MOD 16; hex := Concat(seq[rem + 1], hex); decVal := decVal DIV 16; UNTIL (decVal = 0); WHILE Len(hex) < 2 DO hex := Concat('0', hex); { pad with zero } U_Dec2Hex := hex; END;
{ NNA ************************************************ } { From [http://www.nemetschek.net| Nemetschek] examples. Can someone add the reference? I cannot find it anymore. Where does it come from? } { converts a decimal value into it's hexadecimal equivalent } FUNCTION DecimalToHex(decval: LONGINT; usepound: BOOLEAN):STRING; VAR rem : INTEGER; hexString : STRING; BEGIN hexString := ''; REPEAT rem := decval MOD 16; CASE rem OF 0: hexString := Concat('0', hexString); 1: hexString := Concat('1', hexString); 2: hexString := Concat('2', hexString); 3: hexString := Concat('3', hexString); 4: hexString := Concat('4', hexString); 5: hexString := Concat('5', hexString); 6: hexString := Concat('6', hexString); 7: hexString := Concat('7', hexString); 8: hexString := Concat('8', hexString); 9: hexString := Concat('9', hexString); 10: hexString := Concat('A', hexString); 11: hexString := Concat('B', hexString); 12: hexString := Concat('C', hexString); 13: hexString := Concat('D', hexString); 14: hexString := Concat('E', hexString); 15: hexString := Concat('F', hexString); END; decval := decval DIV 16; UNTIL (decval = 0); IF usepound THEN DecimalToHex := Concat('#', hexString) ELSE DecimalToHex := hexString; END;
[edit] Example
A dialog prompts for entering a longint value. If the value entered is correct, a new dialog will output the value in hex form.
temp_i := IntDialog('Enter a value to be converted to hex string:', '135'); IF NOT DidCancel & (temp_i > -1) & (temp_i < 256) THEN AlrtDialog(U_Dec2Hex(temp_i)) ELSE SysBeep
Categories: Subroutines - Converters | VectorScript Subroutines
VectorScript
VectorScript
VectorScript > VectorScript Example Library
VectorScript > VectorScript Example Library > VectorScript Subroutines
VectorScript > VectorScript Example Library > VectorScript Subroutines > Subroutines - Converters
