Jump to content

  • Log In with Steam Sign In
  • Create Account
187 players in 38 servers, 284 users online, 2 recent bans.

Recent Status Updates

View All Updates

Recent Topics

Featured Partners

Steamid C++ Class Source Code

- - - - -

  • Please log in to reply
1 reply to this topic



    HG Member

  • 61 posts
  • Locationroot@
Here's a SteamID C++ class I wrote to make handling SteamIDs extremely easy.

Constructed with from std::string of a Steam ID in either legacy (STEAM_0:1:xxxxxx) or ID3 (U:1:xxxxxx) format.

Can also be constructed from an 64 bit ID struct, aka the long number in steam community profile urls.

The class takes up only 4 bytes ( sizeof(uint32_t) ) of memory for each SteamID object.

operator== for equality checking against other SteamIDs
operator<< and operator>> for serialization (saving to file or database) and is human readable (std::cout)

SteamIDs constructed from any format can easily be converted with the id() id3() id_i32() and if_i64() functions.
The conversion is built on each function call making the object itself have a footprint of only 4 bytes since based on my observation, the account id (32bits) is the only meaningful part of a SteamID.

The Code:

#pragma once
#include <string>
#include <vector>
#include <cstdint>

// For splitting strings
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
std::vector<std::string> split(const std::string &s, char delim);

// https://developer.valvesoftware.com/wiki/SteamID
// When representing a SteamID in a program you only need
//   32bits to hold the actual account ID. Although valve
//   says that a 64bit data struct is required, the upper
//   32 bits of that struct are always identical for every
//   SteamID
class SteamID {
  // Default constructor for empty SteamID
  // Construct from either SteamID or SteamID3
  SteamID(std::string &strSteamID);
  // Construct from SteadID64
  SteamID(uint64_t &_steamID64);
  // Construct copy
  SteamID(const SteamID& from);
// serialization, for human readable output use id() or id3()
friend std::ostream& operator<<(std::ostream &os, const SteamID& s);
friend std::istream& operator>>(std::istream &is, SteamID& s);
// returns legacy SteamID
std::string id() const;
// returns SteamID3
std::string id3() const;
// returns SteamID64
uint64_t id_i64() const;
// returns account id
uint32_t id_i32() const;
// The internal lower 32bits of SteamID64 (account ID)
uint32_t _ID;

#include "SteamID.hpp"
#include <sstream>
#define ID64_HIGHPART 0x0110000100000000
  : _ID(0)
SteamID::SteamID(std::string &strSteamID)
  : _ID(0)
  std::vector<std::string> IDTokens = split(strSteamID, ':');
  if (IDTokens.size() == 3)
	if (strSteamID[0] == 'S')
	  _ID += strtoll(IDTokens[1].c_str(), nullptr, 10);
	  _ID += (strtoll(IDTokens[2].c_str(), nullptr, 10) * 2);
	else if (strSteamID[0] == 'U')
	  _ID += strtoll(IDTokens[2].c_str(), nullptr, 10);
	  if (!(_ID % 2))
	{ // failed
	  _ID = 0;
  { // failed
	_ID = 0;
SteamID::SteamID(uint64_t &_steamID64)
  if ((_steamID64 & ID64_HIGHPART) == ID64_HIGHPART)
	// cast away upper 32 bits
	_ID = static_cast<uint32_t>(_steamID64);
  { // failed
	_ID = 0;
SteamID::SteamID(const SteamID & from)
: _ID(from._ID)
std::string SteamID::id() const
  std::string strID("STEAM_0:");
  if (_ID % 2)
  strID.append(std::to_string(_ID / 2));
  return strID;
std::string SteamID::id3() const
  std::string strID3("U:1:");
  return strID3;
uint64_t SteamID::id_i64() const
{ return (static_cast<uint64_t>(_ID) | ID64_HIGHPART); }
uint32_t SteamID::id_i32() const
{ return uint32_t(_ID); }
std::ostream& operator<<(std::ostream& os, const SteamID& s)
  os << s._ID;
  return os;
std::istream & operator >> (std::istream & is, SteamID & s)
  is >> s._ID;
  return is;

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
  std::stringstream ss(s);
  std::string item;
  while (std::getline(ss, item, delim))
  return elems;

std::vector<std::string> split(const std::string &s, char delim)
  std::vector<std::string> elems;
  split(s, delim, elems);
  return elems;



    HG Veteran

  • 1,856 posts
  • LocationLmao

If you're interested in any HGDC Development please let me or thomasjosif know.  You seem to know how to code well enough, and I'm sure your skills can be used to help the HGDC team.  If you'd like to learn sourcemod let me know as well.
Posted Image
Thanks to Icon for the amazing signature!
Posted Image
Posted Image
Posted Image

View Postmegan, on January 29 2015 - 01:32 PM, said:

To be honest go ask everyone on jb who plays constantly erryone loves me i've never heard of no weeman and i never see any councils but burnt

Bunbun said:

Everyone always asks me for stuff, but when I ask for nudes everyones like "noooooo im only 13"

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users