Conv-Hex to Decimal

From Vectorlab
Jump to: navigation, search

Conv-Hex to Decimal

Contents

export this page XML wiki

Converts a singular hexadecimal component value into a decimal component value in range 0-255 (8-bit). For example the blue or red or green portion, singularly. See also C_Hex8ToRGB16 for converting a whole hex triplet into an RGB-16 value. By Orso B. Schmid


 Orso ************************************************ }
{ convert hex to integer in range 0-255 }
{ for example
U_HexToDec(9F) 	--> 159
U_HexToDec(00) 	--> 0
U_HexToDec(7E) 	--> 126 }
FUNCTION U_HexToDec(hex: STRING): INTEGER;
	VAR
		c : CHAR;
		i, result : INTEGER;
		values : DYNARRAY OF CHAR;
	 
	BEGIN
		values := '0123456789ABCDEF';
		UprString(hex);
		result := 0;
		
		i := 0;
		WHILE i < Len(hex) DO BEGIN
			i := i + 1;
			c := Copy(hex, i, 1);
			
			IF Pos(c, values) > 0 THEN
				result := 16 * result + Pos(c, values)-1;
				{ bug: if you pass Pos(hexDyna[i], ...) then the script stops without error message }
		END;
		
		U_HexToDec := result;
	END;


Example

The example prompts for entering a string representing a hexadecimal value and returns the corresponding decimal value.

temp_s := StrDialog('Enter an STRING value to be converted to decimal 8-bit color value (in range 0-255):', '9F');
    
    IF NOT didCancel & (Len(temp_s) > 0) THEN
        alrtDialog(concat(U_HexToDec(temp_s)));