SA-MP Forums Archive
Hex Converter - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Hex Converter (/showthread.php?tid=575441)



Hex Converter - ranme15 - 26.05.2015

Hey,

I need to convert this hex code:
Код:
#RRGGBBAA
to:
Код:
0xAARRGGBBB
And please, do not give me these kind of functions:
Код:
stock HexToInt(string[]) // By DracoBlue
{
  	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;
}

stock HexToRGBA(colour, &r, &g, &b, &a) //By Betamaster
{
	r = (colour >> 24) & 0xFF;
	g = (colour >> 16) & 0xFF;
	b = (colour >> 8) & 0xFF;
	a = colour & 0xFF;
}

stock RGBA2ARGB(rgba)
{
    new src[4];
    for(new j = 0; j < 4; j++) src[j] = (!(24-(8*j)) ? rgba : (rgba >> (24-(8*j)))) & 0x000000FF;
    return (src[3] * 16777216) + (src[0] * 65536) + (src[1] * 256) + src[2];
}

stock RGBAToARGB( rgba )
    return rgba >>> 8 | rgba << 24;
I tried these all, never succeed converting it.

Thanks in advance, Ran


Re: Hex Converter - Jefff - 26.05.2015

Its ok
pawn Код:
stock RGBAToARGB( rgba )
    return rgba >>> 8 | rgba << 24;

    new argb = 0xFFFF00FF;
    new col = 0xFF00FFFF;
    printf("%d | %d",argb, RGBAToARGB( col ));
    // result -65281 | -65281



Re: Hex Converter - ranme15 - 26.05.2015

Quote:
Originally Posted by Jefff
Посмотреть сообщение
Its ok
pawn Код:
stock RGBAToARGB( rgba )
    return rgba >>> 8 | rgba << 24;

    new argb = 0xFFFF00FF;
    new col = 0xFF00FFFF;
    printf("%d | %d",argb, RGBAToARGB( col ));
    // result -65281 | -65281
......


Re: Hex Converter - ranme15 - 28.05.2015

Bump.


Re: Hex Converter - Vince - 28.05.2015

For what? You've been given the answer.


Re: Hex Converter - sammp - 28.05.2015

Why exactly would you need this?


Re: Hex Converter - ranme15 - 28.05.2015

Quote:
Originally Posted by Vince
Посмотреть сообщение
For what? You've been given the answer.
Look at it again.
Quote:
Originally Posted by sammp
Посмотреть сообщение
Why exactly would you need this?
I convert #RRGGBBAA colour from a file and need to convert this into 0xAARRGGBB in order to use the SetMaterialObject.


Re: Hex Converter - sammp - 28.05.2015

Quote:
Originally Posted by ranme15
Посмотреть сообщение
Look at it again.

I convert #RRGGBBAA colour from a file and need to convert this into 0xAARRGGBB in order to use the SetMaterialObject.
Tbh converting it manually would have been quicker than posting here :P :P :P


Re: Hex Converter - Jefff - 28.05.2015

Example code
pawn Код:
new hex,line,AARRGGBB;
while(fread(file,str))
{
    if(str[0] == '#')
    {
        strdel(str,0,1);
        strins(str,"0x",0);
    }
    if(sscanf(str,"x",hex))
    {
        printf("error in line %d", ++line);
        continue;
    }
    AARRGGBB = RGBAToARGB( hex ); // your aarrggbb
}