I had been doing some nerdy spring-cleaning and found this while digging through the ‘junk’ folder. Maybe useful so I’m posting it here.

// Convert ASCII Text to Binary
function text2bin($string) {
    $string_array = explode("\r\n", chunk_split($string, 1));
    for ($n = 0; $n < count($string_array) - 1; $n++) {
        $newstring .= substr("0000".base_convert(ord($string_array[$n]), 10, 2), -8);
    }
    $newstring = chunk_split($newstring, 8, " ");
    return $newstring;
}

// Convert Binary to ASCII Text
function bin2text($string) {
    $string = str_replace(" ", "", $string);
    $string_array = explode("\r\n", chunk_split($string, 8));
    for ($n = 0; $n < count($string_array) - 1; $n++) {
        $newstring .= chr(base_convert($string_array[$n], 2, 10));
    }
    return $newstring;
}

// Convert ASCII Text to Hexadecimal
function ascii2hex($string) {
    return chunk_split(bin2hex($string), 2, " ");
}	

// Convert Hexadecimal to ASCII Text
function hex2ascii($string) {
    $string = str_replace(" ", "", $string);
    for ($n=0; $n < strlen($string); $n+=2) {
        $newstring .=  pack("C", hexdec(substr($string, $n, 2)));
    }
    return $newstring;
}

// Convery Binary to Hexadecimal
function binary2hex($string) {
    $string = str_replace(" ", "", $string);
    $string_array = explode("\r\n", chunk_split($string, 8));
    for ($n = 0; $n < count($string_array) - 1; $n++) {
        $newstring .= base_convert($string_array[$n], 2, 16);
    }
    $newstring = chunk_split($newstring, 2, " ");
    return $newstring;
}

// Convert Hexadecimal to Binary
function hex2binary($string) {
    $string = str_replace(" ", "", $string);
    $string_array = explode("\r\n", chunk_split($string, 2));
    for ($n = 0; $n < count($string_array) - 1; $n++) {
        $newstring .= substr("0000".base_convert($string_array[$n], 16, 2), -8);
    }
    $newstring = chunk_split($newstring, 8, " ");
    return $newstring;
}