AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Converting Hexadecimal to Decimal (https://forums.alliedmods.net/showthread.php?t=46216)

DotNetJunkie 10-21-2006 13:40

Converting Hexadecimal to Decimal
 
If you are unfamiliar with hexadecimal numbers please look here:
http://www.permadi.com/tutorial/numHexToDec/

I looked around and didn't find a hexadecimal to decimal conversion function
and I needed one for one of my projects so I wrote one.

Code:

public HexToDec(value[])
{
        new string[32];
        copy(string,sizeof(string)-1,value);
        strtoupper(string);
        new temp = 0;
        new len = strlen(string);
        new i = len;
        while( i > 0 )
        {
                new dec = 0;
                switch( string[i-1] )
                {
                        case 'A': dec = 10;
                        case 'B': dec = 11;
                        case 'C': dec = 12;
                        case 'D': dec = 13;
                        case 'E': dec = 14;
                        case 'F': dec = 15;
                        case '0': dec = 0;
                        case '1': dec = 1;
                        case '2': dec = 2;
                        case '3': dec = 3;
                        case '4': dec = 4;
                        case '5': dec = 5;
                        case '6': dec = 6;
                        case '7': dec = 7;
                        case '8': dec = 8;
                        case '9': dec = 9;
                }
                temp += dec*power(16,(len-i));
                i--;
        }
        return temp;       
}


<<<Anonysmous>>> 01-06-2009 17:18

Re: Converting Hexadecimal to Decimal
 
My dayz This is awesome, dude this helped me alot .... really, not just to make u proud, I used this for some stuff like map originz an all :D!
+k!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

jim_yang 01-06-2009 22:20

Re: Converting Hexadecimal to Decimal
 
Code:

public HexToDec(hex[])
{
    new i, result, value
    while((value = isxdigit(hex[i++])) != -1)
    {
        result = result * 16 + value
    }
    return result
}
 
isxdigit(ch)
{
    if(!ch)
        return -1
   
    if('0' <= ch <= '9')
        return ch - '0'
   
    ch &= ~0x20
    if('A' <= ch <= 'F')
        return ch - 'A' + 10
   
    return -1
}

try this

anakin_cstrike 01-11-2009 16:22

Re: Converting Hexadecimal to Decimal
 
Very nice Jim! thank you, i'll use that in C++
DotNetJunkie, same as @ jim.

sawce 01-19-2009 17:46

Re: Converting Hexadecimal to Decimal
 
Quote:

Originally Posted by anakin_cstrike (Post 742388)
i'll use that in C++

http://www.google.com/search?q=strto...ient=firefox-a


All times are GMT -4. The time now is 21:54.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.