<?php


class Crypter
{
    public function Crypter()
    {

    }

    public function __construct()
    {
    }


    public function SwipeString($raw)
    {
        if (strlen($raw) % 2 == 0) {
            $raw = str_split($raw);
            $out = array();
            for ($i = 0; $i < count($raw); $i += 2) {
                $out[$i] = $raw[$i + 1];
                $out[$i + 1] = $raw[$i];
            }
            return implode($out);
        } else {
            return $raw;
        }
    }

    public function string2ByteArray($string)
    {
        return unpack('C*', $string);
    }

    public function byteArray2String($byteArray)
    {
        $chars = array_map("chr", $byteArray);
        return join($chars);
    }

    public function byteArray2Hex($byteArray)
    {
        $chars = array_map("chr", $byteArray);
        $bin = join($chars);
        return bin2hex($bin);
    }

    public function hex2ByteArray($hexString)
    {
        $string = hex2bin($hexString);
        return unpack('C*', $string);
    }

    public function string2Hex($string)
    {
        return bin2hex($string);
    }

    public function hex2String($hexString)
    {
        return hex2bin($hexString);
    }
}

