PDA

View Full Version : Can someone explain how this function works?


MR_MAN
11/06/2012, 02:44 PM
Basically I'm trying to send the players color as a hex value toa database, using a intTohex function to convert the integer value to hex!

Here is the function

#define IntToDual(%0) IntToBase(%0, 2)
#define IntToOctal(%0) IntToBase(%0, 8)
#define IntToHex(%0) IntToBase(%0, 16)
stock IntToBase(number, const base) {
new str[32];
if(1 < base < 37) {
new
m = 1,
depth = 0;
while (m <= number) {
m *= base;
depth++;
}
for ( ; (--depth) != -1; ) {
str[depth] = (number % base);
number = ((number - str[depth]) / base);
if(str[depth] > 9) str[depth] += 'A'; else str[depth] += '0';
}
}
return str;
}


And here is the code I'm using to send the value to the database

new string[128];
format(string, sizeof(string), "INSERT INTO test (Name,Text,Color) VALUES ('%s','%s','%x')", gPlayerName[playerid], text, IntToBase(GetPlayerColor(playerid)));
mysql_query(string);


However I'm not getting the results I wanted... well none at all! Could someone explain how could I achieve this?

Stevee
11/06/2012, 02:46 PM
Please wrap your code in [code ][/ code] tags (to avoid problems, such as a smiley appearing)

Vince
11/06/2012, 02:47 PM
Sigh. Colors are numbers! Unless you intend to constantly peek and poke in the database manually, you should just save your colors as integers. You will end up with something like -16776961 for the color red, but that's absolutely fine.

MR_MAN
11/06/2012, 02:53 PM
My problem is that I'm trying to use the values in the database to set the color of a html element, so I need it in a hex format!

[NWA]Hannes
11/06/2012, 03:00 PM
Sigh. Colors are numbers! Unless you intend to constantly peek and poke in the database manually, you should just save your colors as integers. You will end up with something like -16776961 for the color red, but that's absolutely fine.
GetPlayerColor returns the colors as numbers like you said.

But how can I use those numbers if I for example want to colour a string or set a players color?