hex-decimal conversion?

Posted By: deianthropus

hex-decimal conversion? - 11/10/10 19:46

Can someone point me to a good hexidecimal-decimal conversion function?

essentially, i need to translate a hex string ex. "a1b2c3" into an RGB vector dynamically. these are not programmed numbers, but entered data; so the conversion must be done during runtime, which is why i'm looking for a function rather than converting them myself.
Posted By: Lukas

Re: hex-decimal conversion? - 11/11/10 14:25

In standard C/C++, there is a function called sscanf, which you could use for that purpose. I don't know if there is any way to use it in Lite-C, maybe you can just make a small DLL for that.
Posted By: Quad

Re: hex-decimal conversion? - 11/11/10 15:12

just wrote one:

hex_to_color.c
Code:
//hex to var, used internally, by hex_to_color
var h2v(char hex){
	switch(hex){
		case '0': return 0; break;
		case '1': return 1; break;
		case '2': return 2; break;
		case '3': return 3; break;
		case '4': return 4; break;
		case '5': return 5; break;
		case '6': return 6; break;
		case '7': return 7; break;
		case '8': return 8; break;
		case '9': return 9; break;
		case 'a':case 'A': return 10; break;
		case 'b':case 'B': return 11; break;
		case 'c':case 'C': return 12; break;
		case 'd':case 'D': return 13; break;
		case 'e':case 'E': return 14; break;
		case 'f':case 'F': return 15; break;
	}
	return 0;
}

COLOR* hex_to_color(char* hc,COLOR* gs_color){
	//hc = hex color, gs_color = color to be changed
	//convert colors to vars and assign to color
	gs_color.red = h2v(hc[0]) * 16 + h2v(hc[1]);
	gs_color.green = h2v(hc[2]) * 16 + h2v(hc[3]);
	gs_color.blue = h2v(hc[4]) * 16 + h2v(hc[5]);
	return gs_color;
}



and a script to test it:
press e and input a hex RGB value like DFc1D0 , both uppercase and lowercase works
test.c
Code:
#include <acknex.h>
#include <hex_to_color.c>
STRING* input_str = "000000";
TEXT* input_txt={
	font = "Arial#24";
	pos_x = 20;
	pos_y = 20;
	string(input_str);
	flags = SHOW;
}

void get_color(){
	inkey(input_str);
	hex_to_color(_chr(input_str),screen_color);
}

void main(){
	while(!key_esc){
		on_e = get_color;
		wait(1);
	}
	sys_exit("");
}


© 2024 lite-C Forums