Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (Ayumi), 1,088 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
hex-decimal conversion? #346941
11/10/10 19:46
11/10/10 19:46
Joined: Apr 2010
Posts: 38
D
deianthropus Offline OP
Newbie
deianthropus  Offline OP
Newbie
D

Joined: Apr 2010
Posts: 38
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.

Re: hex-decimal conversion? [Re: deianthropus] #347009
11/11/10 14:25
11/11/10 14:25
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
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.

Re: hex-decimal conversion? [Re: Lukas] #347012
11/11/10 15:12
11/11/10 15:12
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
Quad Offline
Senior Expert
Quad  Offline
Senior Expert

Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
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("");
}



Last edited by Quadraxas; 11/11/10 15:14.

3333333333

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1