AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Convert STEAMID to Steam Community ID (https://forums.alliedmods.net/showthread.php?t=60899)

pokemonmaster 07-15-2016 17:10

Re: Convert STEAMID to Steam Community ID
 
This is a snippet I wrote for sourcepawn, but I won't use anymore.
Will convert Steam64 string to steam32 int

Don't even know if this will be of use for anyone.
This was built based on this
PHP Code:

// 64 bit max is 19 numbers + 1 (for teminator)
#define MAX_STEAM64_LENGTH 20

// 11 numbers is the total numbers that the 32 bit can hold, lets make sure its less than that by 1.
// 10 + terminator
#define MAX_INTEGERS 11

stock bool GetSteam32FromSteam64(const char szSteam64Original[MAX_STEAM64_LENGTH], int &iSteam32)
{
    
char szSteam32[MAX_STEAM64_LENGTH];
    
char szSteam64[MAX_STEAM64_LENGTH];
    
    
// We don't want to actually edit the original string.
    // We make a new string for the editing.
    
strcopy(szSteam64sizeof szSteam64szSteam64Original);
    
    
// Remove the first three numbers
    
ReplaceStringEx(szSteam64sizeof(szSteam64), "765""");
    
    
// Because pawn does not support numbers bigger than 2147483647, we will need to subtract using a combination of numbers.
    // The combination can be
    // 1 number to MAX_INTEGERS
    
char szSubtractionString[] = "61197960265728";
    
    
// First integer is the integer from szSteam64
    // Second is from the subtraction string szSubtractionString
    
char szFirstInteger[MAX_INTEGERS], szSecondInteger[MAX_INTEGERS];
    
int iFirstIntegeriSecondInteger;
    
    
char szResultInt[MAX_INTEGERS];
    
    
// Ugly hack
    // Make the strings 00000000000 so when we use StringToInt the zeroes won't affect the result
    // sizeof - 1 because we need the last End of string (0) byte;
    
SetStringZeros(szFirstIntegersizeof(szFirstInteger), sizeof(szFirstInteger) - 1);
    
SetStringZeros(szSecondIntegersizeof(szSecondInteger), sizeof(szSecondInteger) - 1);

    
// Start from the end of the string, because subtraction should always start from the first number in the right.
    
int iResultInt;
    
    
int iSteam64Position strlen(szSteam64);
    
int iIntegerPosition strlen(szFirstInteger);
    
    
int iNumCount;
    
int iResultLen;
    
char szStringZeroes[MAX_INTEGERS];
    
    while(--
iSteam64Position > -1)
    {
        
iIntegerPosition -= 1;
        
        ++
iNumCount;
        
szFirstInteger[iIntegerPosition] = szSteam64[iSteam64Position];
        
szSecondInteger[iIntegerPosition] = szSubtractionString[iSteam64Position];
        
        
iFirstInteger StringToInt(szFirstInteger);
        
iSecondInteger StringToInt(szSecondInteger);
            
        
// Can we subtract without getting a negative number?
        
if(iFirstInteger >= iSecondInteger)
        {
            
iResultInt iFirstInteger iSecondInteger;
            
// 69056897
            
PrintToServer("Subtract %s from %s = %d"szFirstIntegerszSecondIntegeriResultInt);
            if(
iResultInt)
            {
                
IntToString(iResultIntszResultIntsizeof szResultInt);
                
                if( 
iNumCount != (iResultLen  strlen(szResultInt) ) )
                {
                    
SetStringZeros(szStringZeroessizeof szStringZeroesiNumCount iResultLen);
                }
                
                else
                {
                    
szStringZeroes "";
                }
            }
            
            else
            {
                
szResultInt "";
                
                
SetStringZeros(szStringZeroessizeof szStringZeroesiNumCount);
                
PrintToServer("String Zeroes: %s"szResultInt);
            }
            
            
Format(szSteam32sizeof szSteam32"%s%s%s"szStringZeroesszResultIntszSteam32);
            
PrintToServer("Current Progress: %s"szSteam32);
            
            
// Reset our stuff.
            
SetStringZeros(szFirstIntegersizeof(szFirstInteger), sizeof(szFirstInteger) - 1);
            
SetStringZeros(szSecondIntegersizeof(szSecondInteger), sizeof(szSecondInteger) - 1);
            
            
iIntegerPosition strlen(szFirstInteger);
            
iNumCount 0;
        }
        
        if(
iIntegerPosition 0)
        {
            
// We failed, and this calculation can not be done in pawn.
            
return false;
        }
        
        
// if not, lets add more numbers.
    
}
    
    
iSteam32 StringToInt(szSteam32);
    return 
true;
}

void SetStringZeros(char[] szStringint iSizeint iNumZeros)
{
    
int i;
    for(
0iNumZeros && iSizei++)
    {
        
szString[i] = '0';
    }
    
    
szString[i] = 0;



Byte 04-14-2017 23:31

Re: Convert STEAMID to Steam Community ID
 
Don't mean to bump an oldish thread but this might save someone a tiny bit of time later....

Here is the Sourcemod adaption of YamiKaitou's code here with asherkin's tip:
https://forums.alliedmods.net/showpo...&postcount=341
https://forums.alliedmods.net/showpo...&postcount=342

So far its passed all my tests but let me know if you find any issues.

PHP Code:

stock void GetSteamId2(const char[] szSteam64char[] szSteam2int iLen)
{
  
char szBase[18] = "76561197960265728";
  
int iBorrow 0;
  
char szSteam[18];
  
char szAccount[18];
  
int iY 0;
  
int iZ 0;
  
int iTemp 0;
  
  
//Copy steamid64 Over
  
Format(szSteamsizeof(szSteam), szSteam64);
  
  
//Determine iY
  
if (intval(szSteam[16]) % == 1) {
    
iY 1;
    
szSteam[16] = strval(intval(szSteam[16]) - 1);
  }
  
  
//Determine szAccount
  
for (int k 16>= 0; --k) {
    if (
iBorrow 0) {
      
iTemp intval(szSteam[k]) - 1;
      
      if (
iTemp >= intval(szBase[k])) {
        
iBorrow 0;
        
szAccount[k] = strval(iTemp intval(szBase[k]));
      }
      else {
        
iBorrow 1;
        
szAccount[k] = strval((iTemp 10) - intval(szBase[k]));
      }
    }
    else {
      if (
intval(szSteam[k]) >= intval(szBase[k])) {
        
iBorrow 0;
        
szAccount[k] = strval(intval(szSteam[k]) - intval(szBase[k]));
      }
      else {
        
iBorrow 1;
        
szAccount[k] = strval((intval(szSteam[k]) + 10) - intval(szBase[k]));
      }
    }
  }
  
  
//Divide szAccount answer by 2
  
iZ StringToInt(szAccount) / 2;
  
  
//Construct final steam2 id
  
Format(szSteam2iLen"STEAM_1:%d:%d"iYiZ);
}

int strval(const int iNum)
{
  return 
'0' + ((iNum >= && iNum <= 9) ? iNum 0);
}

int intval(int cNum)
{
  return (
cNum >= '0' && cNum <= '9') ? (cNum '0') : 0;



waylander3 08-05-2017 14:36

Re: Convert STEAMID to Steam Community ID
 
Hey guys, how can i convert U:1:0 to STEAM:1:0?
From steamID3 to steamID

DarkDeviL 08-06-2017 17:07

Re: Convert STEAMID to Steam Community ID
 
Quote:

Originally Posted by waylander3 (Post 2539802)
Hey guys, how can i convert U:1:0 to STEAM:1:0?
From steamID3 to steamID

From "[U:1:1532443]":

1532443 / 2 = 766221,5

Since the number is odd, it is then "STEAM_1:1:766221".

If the number was didn't give an odd number, e.g. "766221", it would have been "STEAM_1:0:766221"

waylander3 08-06-2017 20:14

Re: Convert STEAMID to Steam Community ID
 
Quote:

Originally Posted by arne1288 (Post 2539999)
From "[U:1:1532443]":

1532443 / 2 = 766221,5

Since the number is odd, it is then "STEAM_1:1:766221".

If the number was didn't give an odd number, e.g. "766221", it would have been "STEAM_1:0:766221"

No, i mean on server. On my server (dota 2) i have U: format (in "status"), and i need STEAM: format.

DarkDeviL 08-07-2017 08:09

Re: Convert STEAMID to Steam Community ID
 
Quote:

Originally Posted by waylander3 (Post 2540029)
No, i mean on server. On my server (dota 2) i have U: format (in "status"), and i need STEAM: format.

It sounded like you wanted the formula for converting it..

If you want the "status" command to show different output, you need to create a plugin that does it exactly as you want.

If you have any other plugins showing the one where you want the other, you need to adjust those plugins yourself, or hire someone to do it for you.

Powerlord 09-27-2017 02:05

Re: Convert STEAMID to Steam Community ID
 
Note that the number 76561197960265728 looks like a constant, but it really isn't.

This doesn't really matter if you're converting SteamId2 to SteamId64 because SteamId2 only supports individual user accounts. However, SteamId3 also contains the account type and (optionally) instance ID.

The correct calculation for Community IDs:

Code:

((universe << 56) | (accountType << 52) | (instance << 32) | accountId)
The first 3 default to 1 for individual user accounts, which means the "magic number" for individual accounts is

Code:

((1L << 56) | (1L << 52) | (1L << 32))
which is 76561197960265728.

(L is there because 64-bit literals need it in the language I was using to double check this.)

If you try this to get group profile IDs or game server profile IDs, it fails spectacularly because they use different account types and instances.

Side note: The second section of a SteamId2 is the lowest bit of the account ID and the third section is the remaining 31 bits of the account ID. Hence, to get the account ID, it's

((third section << 1) | second section)

rautamiekka 09-27-2017 02:25

Re: Convert STEAMID to Steam Community ID
 
Quote:

Originally Posted by Powerlord (Post 2551105)
Note that the number 76561197960265728 looks like a constant, but it really isn't.

This doesn't really matter if you're converting SteamId2 to SteamId64 because SteamId2 only supports individual user accounts. However, SteamId3 also contains the account type and (optionally) instance ID.

The correct calculation for Community IDs:

Code:

((universe << 56) | (accountType << 52) | (instance << 32) | accountId)
The first 3 default to 1 for individual user accounts, which means the "magic number" for individual accounts is

Code:

((1L << 56) | (1L << 52) | (1L << 32))
which is 76561197960265728.

(L is there because 64-bit literals need it in the language I was using to double check this.)

If you try this to get group profile IDs or game server profile IDs, it fails spectacularly because they use different account types and instances.

Side note: The second section of a SteamId2 is the lowest bit of the account ID and the third section is the remaining 31 bits of the account ID. Hence, to get the account ID, it's

((third section << 1) | second section)

Very good to know, thank you a lot.

4ever16 03-12-2020 07:19

Re: Convert STEAMID to Steam Community ID
 
Quote:

Originally Posted by DarthNinja (Post 999238)
Here's some php I wrote to calculate Steam Community IDs using php's basic math functions.
(No php extensions required :o)


PHP Code:

//Get SteamID and save it to $steam_id somehow

<?php
$steam_id
=strtolower($steamid);
if (
substr($steam_id,0,7)=='steam_0') {
$tmp=explode(':',$steam_id);
}
if ((
count($tmp)==3) && is_numeric($tmp[1]) && is_numeric($tmp[2])){
 
 
//The 'Magic Number': 76561197960265728
 //Split up so it can be calculated without an extension.
 

 
$steamidCalc=($tmp[2]*2)+$tmp[1]; //Work out step 1
$calckey='1197960265728'//Second bit of the magic number
$pre='7656'//First bit of the magic number
    
$steamcid=$steamidCalc+$calckey//works out the ending of the steam community ID
    
$link="http://steamcommunity.com/profiles/";
echo 
$link; echo $pre; echo $steamcid;
 
 };

?>

By breaking off a few digit, it prevents php from outputting the number using scientific notation.
This does however, mean that this will have to be updated once there are enough steam accounts to make the numbers roll over.

Hope that's helpful to someone :wink:

Thanks!


All times are GMT -4. The time now is 06:00.

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