#native_company# #native_desc#
#native_cta#

punch card convertor

By Edwin Groothuis
on September 4, 2001

Version: 1.0

Type: Function

Category: Algorithms

License: BSD License

Description: Convert strings into punch cards

<?
    //
    // BCD Convertor v1.0 by Edwin Groothuis ([email protected])
    //
    // $Id: php-bcd.php,v 1.1 2001/09/04 12:01:19 mavetju Exp $
    //
    // If you didn't get this file via http://www.mavetju.org, please
    // check for the availability of newer versions.
    //
    // See LICENSE for distribution issues. If this file isn't in
    // the distribution, please inform me about it.
    //
    // Feel free to use this yourself.
    //

    //
    // Usage:
    //
    // to_bcd(string)
    //

    $holes=array(
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x0,         0x206,   0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,
    0x812,       0x412,   0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,
    0x200,       0x100,   0x080,   0x040,   0x020,   0x010,   0x008,   0x004,
    0x002,       0x001,   0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,
    0x022,       0x900,   0x880,   0x840,   0x820,   0x810,   0x808,   0x804,
    0x802,       0x801,   0x500,   0x480,   0x440,   0x420,   0x410,   0x408,
    0x404,       0x402,   0x401,   0x280,   0x240,   0x220,   0x210,   0x208,
    0x204,       0x202,   0x201,   0x082,   0x822,   0x600,   0x282,   0x30f,
    0x900,       0x880,   0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
    0x801,       0x500,   0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
    0x402,       0x401,   0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
    0x202,       0x201,   0x082,   0x806,   0x822,   0x600,   0x282,   0x0,
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x0,         0x0,     0x0,     0x0,     0x0,     0x0,     0x0,     0x0,
    0x206,       0x20a,   0x042,   0x442,   0x222,   0x800,   0x406,   0x812,
    0x412,       0x422,   0xa00,   0x242,   0x400,   0x842,   0x300,   0x200,
    0x100,       0x080,   0x040,   0x020,   0x010,   0x008,   0x004,   0x002,
    0x001,       0x012,   0x40a,   0x80a,   0x212,   0x00a,   0x006,   0x022,
    0x900,       0x880,   0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
    0x801,       0x500,   0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
    0x402,       0x401,   0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
    0x202,       0x201,   0x082,   0x806,   0x822,   0x600,   0x282,   0x30f,
    0x900,       0x880,   0x840,   0x820,   0x810,   0x808,   0x804,   0x802,
    0x801,       0x500,   0x480,   0x440,   0x420,   0x410,   0x408,   0x404,
    0x402,       0x401,   0x280,   0x240,   0x220,   0x210,   0x208,   0x204,
    0x202,       0x201,   0x082,   0x806,   0x822,   0x600,   0x282,   0x0
    );

    function to_bcd($string) {
	global $holes;

	$COLUMNS=48;
	$string=strtoupper($string);
	$rowchars="...123456789";

	$ret= " ________________________________________________n";
	$ret.=sprintf("/%-48s|n",$string);

	for ($row=0;$row<12;$row++) {
	    $ret.="|";
	    for ($i=0;$i<strlen($string);$i++) {
		if (($holes[ord($string[$i])] & 1<<(11-$row))!=0)
		    $ret.=" ";
		else
		    $ret.=$rowchars[$row];
	    }
	    while ($i++ < $COLUMNS)
		$ret.=$rowchars[$row];
	    $ret.="|n";
	}
	$ret.="|________________________________________________|n";
	return $ret;
    }

    if ($text) {
	echo "<pre>",to_bcd($text),"n";
    }
?>

<form method="post" action="<? echo $PHP_SELF; ?>">
<input type="text" name="text" value="<? echo $text; ?>">
</form>