This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Encoding/Decoding Of Script String Identifier To UINT64 word
  Submitted by



This is a code which encodes/decodes a script identifier string into UINT64 word, with the idea that instead of comparing strings or searching ones, you can do that with 64bit words. It accepts case-insensitive identfiers with maximum length of 12 characters, identifiers cannot have underbars. For examples - let you have some resource with lots of files or pictures, and their names doesn't exceed 12 characters, instead of loading them by name, you can go with these two functions. I will use this code for a scripting system, and by this way i'll keep only 64bits per identifier, instead of full-string. Also this will decrease number of errors, which may occur when working with strings. Encoding is lossless - it's not a hash function - you can always decode/encode your identifier. Additionally for each identifier you can specify sub-identifier (0, 1 or 2). I'm using this feature also. It just happened that there is enough space and for it. Any feedback or ideas will be welcome. (malkia)

Download Associated File: code64.c (1,820 bytes)

#if defined(_MSC_VER)
	typedef unsigned __int64 UINT64;
#else
	typedef unsigned long long UINT64;
#endif

/* ---------------------------------------------------------- Encodes/Decodes case-insensitive identfier (maximum length 12 characters) to UINT64 dword ---------------------------------------------------------- syntax: identifier = symbol | identifier [symbol | digit] symbol = 'A'..'Z' | 'a'..'z'; digit = '0'..'9';

With simple words: identifier can't start with DIGITS... :))

Examples: "drumBaseSet0" "silentScope133" "silentScope443" "mainFrame0" "mainFrame1" Illegal examples: "drumBaseSet12" -> more than 12 characters!! (last one will be ignored)

Also for each identifier you can have integer subId (0, 1 or 2) like: "playerID" : 1 "goalieID" : 2 "passerID" : 0

---------------------------------------------------------------------- malkia@mindless.com http://easternAnalog.hypermart.net/ */


UINT64 EncodeId( char *buf, int subId ) { UINT64 code; int digit, i, ch;

if( (unsigned)subId >= 3 ) return 0;

ch = *buf++; if( ch >= 'A' && ch <= 'Z' ) code = ch - 'A' + 1; else if( ch >= 'a' && ch <= 'z' ) code = ch - 'a' + 1; else return 0;

for( i=0; i<11; i++ ) { if( ch ) ch = *buf++;

if( ch == 0 ) digit = ch; else if( ch >= 'A' && ch <= 'Z' ) digit = ch - 'A' + 1; else if( ch >= 'a' && ch <= 'z' ) digit = ch - 'a' + 1; else if( ch >= '0' && ch <= '9' ) digit = ch - '0' + 1 + 26; else return 0;

code = code*37 + digit; }

code = code*3 + subId; return code; }

int DecodeId( UINT64 code, char buf[13] ) { int i, ch, subId;

subId = (int)(code%3); code /= 3;

i = 12; buf[12] = 0; while( i ) { ch = (int)(code%37); code /= 37; buf[--i] = (char)(ch?(ch - 1 + (ch<26?'A':'0')):0); }

return subId; }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.