StringToHex
#1

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.

pawn Код:
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 !
Reply
#2

https://sampwiki.blast.hk/wiki/Colors#Co...alue_with_pawn
Not sure why you'd possibly ever need it. People have some odd views on colors, thinking they are strings and shit.
Reply
#3

Thanks for reply, but it doesn't work correctly. I don't know why, here is my test results:

pawn Код:
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 .
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
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?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)