SA-MP Forums Archive
String into Hex - 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: String into Hex (/showthread.php?tid=600960)



String into Hex - DevinPatino - 15.02.2016

I'm wayy to tired I've been searching for this I might be over-seeing this or something, but I basically I want to change a string (color) into hex (i.e FF0000) is this even possible? If so please give me the code below much love *kiss*


Re: String into Hex - Trucido - 15.02.2016

lmao do you mean like "black" and then getting black in hex? dont bother


Re: String into Hex - -CaRRoT - 15.02.2016

Quote:
Originally Posted by Trucido
Посмотреть сообщение
lmao do you mean like "black" and then getting black in hex? dont bother
How about you not bother posting at all?

@OP - I used to use these 2 functions.

Код:
stock ShiftRGBAToABGR(color)
{
    new r, g, b, a, value = color;
    r = (value >>> 24);
    g = (value >>> 16 & 0xFF);
    b = (value >>> 8 & 0xFF);
    a = (value  & 0xFF);
    value = (a & 0xFF) | ((b & 0xFF) << 8) | ((g & 0xFF) << 16) | (r << 24);
    return value;
}

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;
}



Re: String into Hex - SickAttack - 15.02.2016

pawn Код:
// ** INCLUDES

#include <a_samp>
#include <sscanf>

// ** DEFINES

// *** GENERAL

#define HEX_VALUE "0xFFFFFFFF"
#define INTEGER_VALUE -1

// *** FUNCTIONS

#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)

// ** MAIN

main()
{
    print("Loaded \"color_hex_int.amx\".");

    new string[11], answer[12];
    printf("%s to integer: %i", HEX_VALUE, HexToInt(HEX_VALUE));
    printf("%d to hexadecimal: %s", INTEGER_VALUE, IntToHex(INTEGER_VALUE));

    strcpy(string, HEX_VALUE, 11);
    answer = (IsHexOrInt(string)) ? ("Hexadecimal") : ("Integer");

    printf("Is %s an integer or a hexadecimal value? %s.", string, answer);

    valstr(string, INTEGER_VALUE);
    answer = (IsHexOrInt(string)) ? ("Hexadecimal") : ("Integer");

    printf("Is %s an integer or a hexadecimal value? %s.", string, answer);
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** FUNCTIONS

stock HexToInt(hex[])
{
    new int;
    sscanf(hex, "x", int);
    return int;
}

stock IntToHex(int)
{
    new string[7];
    format(string, sizeof(string), "%06x", int >>> 8);
    return string;
}

stock IsNumeric(const string[])
{
    return !sscanf(string, "{d}");
}

stock IsHexOrInt(hex[])
{
    if(IsNumeric(hex))
    {
        return false;
    }
    return true;
}



Re: String into Hex - [WSF]ThA_Devil - 15.02.2016

A safe method to turn string into hex is to use sscanf plugin using 'x' specifier.
Код:
sscanf( string, "x", hexcol)



Re: String into Hex - AbyssMorgan - 15.02.2016

PHP код:
GetColor(0xFF0000FF);        //Return {FF0000} 
RGB.inc

such a thing needed?