// sym.c MR 13/12/99 // // Pack a 6-character string/word inside an unsigned int. // /* ---------------------------------------------------------------------- Copyright (C) 1999 Menno Rubingh This source code / program is free software; you are allowed to redistribute it and/or to modify it under the terms of the GNU Public Licence (GNU GPL) as published by the Free Software Foundation. See the file 'COPYING' which should have accompanied this source file. This program comes with ABSOLUTELY NO WARRANTY. ---------------------------------------------------------------------- */ #include "sym.h" /* Values (''mod 40''): 0 = blank or 'null' 1 = '.' 2 = '?' 3 = '-' 4...13 = '0'...'9' 14...39 = 'a'...'z' */ sym_t str2sym( const char * pstr ) //Any blanks or illegal characters in pstr are disregarded. //Case-insensitive. //When input string too long, chars at its end are disregarded. { sym_t s = 0; int i = 0; while ( pstr[i] != '\0' && s < 40*40*40*40*40 ) { if ( pstr[i] == '.' ) { s *= 40; s += 1; } else if ( pstr[i] == '?' ) { s *= 40; s += 2; } else if ( pstr[i] == '-' ) { s *= 40; s += 3; } else if ( '0' <= pstr[i] && pstr[i] <= '9' ) { s *= 40; s += pstr[i] - '0' + 4; } else if ( 'a' <= pstr[i] && pstr[i] <= 'z' ) { s *= 40; s += pstr[i] - 'a' + 14; } else if ( 'A' <= pstr[i] && pstr[i] <= 'Z' ) { s *= 40; s += pstr[i] - 'A' + 14; } i++; } return s; } static char sym2strbuf[7]; const char * sym2str( sym_t s ) { int i = 0; sym_t m = 40 * 40 * 40 * 40 * 40; while ( m > s ) { m /= 40; } while ( m > 0 ) { sym_t rest = s / m; if ( rest == 1 ) { sym2strbuf[i++] = '.'; } else if ( rest == 2 ) { sym2strbuf[i++] = '?'; } else if ( rest == 3 ) { sym2strbuf[i++] = '-'; } else if ( 4 <= rest && rest <= 13 ) { sym2strbuf[i++] = rest - 4 + '0'; } else if ( 14 <= rest ) { sym2strbuf[i++] = rest - 14 + 'a'; } s -= ( rest * m ); m /= 40; } sym2strbuf[i] = '\0'; return sym2strbuf; }