PDA

View Full Version : StringToHex


zgintasz
13/06/2012, 10:51 AM
Hi guys,

I searched it, but I didn't find. I need function, that converts string to hex. I know, it's possible with sscanf, but I need to do that WITHOUT sscanf.


new
string[ 50 ],
hex
;
format( string, sizeof( string ), "%x%x", 0x000000 , 255 );
sscanf( string, "x", hex );
printf( "%d %x", hex, hex );

I really need this...

Thanks :)!

Vince
13/06/2012, 10:54 AM
http://wiki.sa-mp.com/wiki/Colors#Convert_string_to_value_with_pawn
Not sure why you'd possibly ever need it. People have some odd views on colors, thinking they are strings and shit.

zgintasz
13/06/2012, 11:07 AM
Thanks for reply, but it doesn't work correctly. I don't know why, here is my test results:


new
string[ 50 ],
hex
;
format( string, sizeof( string ), "%x%x", 0xFF0000, 17 );
sscanf( string, "x", hex );
printf( "sscanf: %d %x", hex, hex );
hex = HexToInt( string );
printf( "HexToInt: %d %x", hex, hex );

// above
stock HexToInt(string[])
{
if (string[0] == 0)
{
return 0;
}
new i;
new cur = 1;
new res = 0;
for (i = strlen(string); i > 0; i--)
{
if (string[i-1] < 58)
{
res = res + cur * (string[i - 1] - 48);
}
else
{
res = res + cur * (string[i-1] - 65 + 10);
cur = cur * 16;
}
}
return res;
}


[13:13:41] sscanf: -16777199 -FFFFEF
[13:13:41] HexToInt: 257 101

In this case, sscanf works properly. Don't ask, why I need it :D.

MP2
13/06/2012, 11:18 AM
Not sure why you'd possibly ever need it. People have some odd views on colors, thinking they are strings and shit.
For in-game input?