Check if hex code is valid
#1

Hello. I'm looking for a funtion, that will check if hex code is valid 'color code'.

For example: FF0097 is valid color code.

GGGGGG isn't valid code.

Regards.
Reply
#2

why do you need that
Reply
#3

I take it you have a string input ?
Reply
#4

Have you tried the x or h parameter in sscanf?
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
If it isn't valid, it won't compile.
This!

Or It will crash the server on runtime if an invalid hex code is used.
Reply
#6

Yh. In fact I was rewriting MTA's color chat. And I already wrote my own function, so if anybody need it, here it goes:

Код:
stock isValidHexCode(code[])
{
	new checked;
	for(new i;i<6;i++)
	{
		if(IsNumeric(code[i])) checked+=1;
		else if((code[i] == 'A' || code[i] == 'a') || (code[i] == 'B' || code[i] == 'b') || (code[i] == 'C' || code[i] == 'c') || (code[i] == 'D' || code[i] == 'd') || (code[i] == 'E' || code[i] == 'e') || (code[i] == 'F' || code[i] == 'f')) checked+=1;
	}
	
	if(checked == 6) return true;
	return false;
}
It works for 6 letters in hex code, so you have to input 6 letters string into function for check.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)