Skip to content

Instantly share code, notes, and snippets.

@awstanley
Created June 11, 2014 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awstanley/f137c75d30bde8eaff8d to your computer and use it in GitHub Desktop.
Save awstanley/f137c75d30bde8eaff8d to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2006-2014 A.W. Stanley
* Released into the public domain as of
* http://awsta.nley.org/post/88514503005/c-steamid-conversion
*
* An assumption is made about "normal" string inputs.
* Note: 76561197960265728 = 0000 0001 0001 0000 0000 0000 0000 0001
* Universe: (0000 0001)
* Account Type: (0001)
* Instance: (0000 0000 0000 0001)
*
* The original code only contained STEAM_A:B:C conversion to universe,
* made no note of assumptions, and did not do the reverse conversion.
*
* Changelog (by year):
* - Jul 2014
* Cleaned up code for release.
* Updated names for cleanliness.
*
* - Aug 2010
* Added bitmask (removing ugly bit manipulation)
*
* - Nov 2007
* New Steam account created.
* Realised it was 64 bit divided into 32 bit + 32 bit
* (fixing original mistake based on common SteamID from friends)
*
* - Nov 2006
* Initial common bits noticed and initial (horrible) code written.
*
*/
#pragma once
#include <stdio.h>
#include <string>
namespace Steam
{
struct _SteamID32_Info {
unsigned int instance : 20;
unsigned int type : 4;
unsigned int universe : 8;
};
struct _SteamID32 {
unsigned int AccountID;
_SteamID32_Info Info;
};
union SteamID {
unsigned long long SteamID64;
_SteamID32 SteamID32;
};
struct SteamInfo
{
SteamID id;
std::string id_string;
void init(unsigned long long i)
{
id.SteamID64 = i;
char s[64];
int sid = id.SteamID32.AccountID % 2;
sprintf(s, "STEAM_0:%i:%i", sid, (id.SteamID32.AccountID-sid)/2);
id_string = s;
}
// You should use this
SteamInfo(unsigned long long id)
{
init(id);
}
// Or this and then subsequently init(id)
SteamInfo() {}
// Assumes the account is a normal public instance
SteamInfo(std::string i)
{
int sid, uni, ser;
sscanf(i.c_str(), "STEAM_%i:%i:%i",uni,ser,sid);
init((sid * 2) + 76561197960265728 + ser);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment