function dce_convert($data, $input_format, $output_format = "none")
{
    log_add('<br><strong><span style="background-color:magenta;">Beginning conversion.</span><br><br>State:</strong><br><br><span style="background-color:skyblue;">Input format: ' . $input_format . '<br>Output format: ' . $output_format . '<br>Data: ' . $data . '</span><br>');
    log_add('<br><span style="background-color:magenta;"><strong>Beginning first step: If the input format and the output format are the same, return the input data.</strong></span><br>');
    if ($input_format == $output_format && $input_format != 'dc') {
        return $data;
    } else {
    }
    log_add('<br><span style="background-color:magenta;"><strong>Beginning second step: Detect one-step conversions (translators that have not been updated to the new system) and redirect them.</strong></span><br>');
    $one_steps = array('3_0a_old' => 'none', 'dce2txt' => 'none', 'dce2hex' => 'none', 'hex2dce' => 'none', 'legacy_cdce' => 'html_snippet', 'legacy_cdce' => 'html');
    if (array_key_exists($input_format, $one_steps)) {
        if ($one_steps[$input_format] == $output_format) {
            $onestep = true;
        } else {
            $onestep = false;
        }
    } else {
        $onestep = false;
    }
    log_add('<br><span style="background-color:magenta;"><strong>Beginning third step: If one-step conversions apply, perform them.</strong></span><br>');
    if ($onestep) {
        $onestep_function = 'onestep' . $input_format . '_to_' . $output_format;
        return $onestep_function($data);
    } else {
        log_add('<br><span style="background-color:magenta;"><strong>Beginning fourth step: Otherwise, convert the data to a Dc list.</strong></span><br>');
        $x_to_dc_function = 'convert_' . $input_format . '_to_dc';
        if (!function_exists($x_to_dc_function)) {
            return 'Unknown input format.';
            error_add('<font color="red">Error! Unknown input format.</font>');
        }
        $dc = $x_to_dc_function($data);
        $dc = preg_replace('/,\\Z/', '', $dc);
        $dc = str_replace(',,', ',0,', $dc);
        log_add('<br><span style="background-color:magenta;"><strong>Beginning fifth step: Convert the data to the chosen output format, and return a value.</strong></span><br>');
        $dc_to_x_function = 'convert_dc_to_' . $output_format . '_output';
        if (!function_exists($dc_to_x_function)) {
            return 'Unknown output format.';
            error_add('<font color="red">Error! Unknown output format.</font>');
        }
        return $dc_to_x_function($dc);
        log_add('<br><span style="background-color:magenta;"><strong>Finished conversion.</strong></span><br>');
    }
}
        log_add('<br><span style="background-color:magenta;"><strong>Beginning fifth step: Convert the data to the chosen output format, and return a value.</strong></span><br>');
        $dc_to_x_function = 'convert_dc_to_' . $output_format . '_output';
        return $dc_to_x_function($dc);
        log_add('<br><span style="background-color:magenta;"><strong>Finished conversion.</strong></span><br>');
    }
}
//TESTS HAPPEN HERE
if ($tests) {
    libdce_tests();
    log_add($test_results);
}
//THIS SHOULD ALL BE AT THE END OF THE SCRIPT
if ($error_happened) {
    /*if (strpos($error_list, 'Errors were encountered during processing') !== false) {
      } else {*/
    error_add('<br><font color="red"><strong>' . $error_counter . ' errors were encountered during processing! Review the above list of error messages and/or the log for more information.</strong></font><br> Note that if you are running the tests, some errors are normal.');
    //}
}
if ($errors) {
    if ($debug) {
        log_add('<h2>Error list:</h2>' . $error_list);
    } else {
        if (strlen($error_list) < 1) {
        } else {
            echo '<h2>Error list:</h2>' . $error_list;
        }
    }
} else {
    if ($debug) {
        log_add('<h2>Error list:</h2>' . $error_list);
    } else {
function convert_3_0a_to_dc($data)
{
    $hex = bin2hex($data);
    if (substr($hex, 12, 2) !== '01') {
        error_add('<font color="red">Error! This document is not stored using the correct version of DCE.</font>');
        return 'This document is not stored using the correct version of DCE.';
    }
    //######################################################
    //This is a DCE 3.0a file.
    global $DcMap_dce3_0a_Core;
    $counter = 14;
    $txt = '';
    while ($counter < strlen($hex)) {
        $txt = $txt . $DcMap_dce3_0a_Core[strtoupper(substr($hex, $counter, 2))] . ',';
        if (substr($hex, $counter, 4) == 'fd03') {
            break;
        }
        $counter = $counter + 2;
    }
    $txt = substr($txt, 3, strlen($txt) - 6);
    return $txt;
}
function convert_3_01a_to_dc($data)
{
    //This function needs to be improved to fully support DCE 3.01a!
    $hex = bin2hex($data);
    if (substr($hex, 0, 12) !== '444345650201') {
        error_add('<font color="red">Error! This document is not stored using the specified format. </font>' . 'The substring "' . substr($hex, 0, 12) . '" should be "444345650201".');
        return 'This document is not stored using the specified format.';
    }
    if (substr($hex, 12, 2) !== '02') {
        error_add('<font color="red">Error! This document is not stored using the specified version of DCE.</font>');
        return 'This document is not stored using the specified version of DCE.';
    }
    //######################################################
    //This is a DCE 3.01a file.
    global $DcMap_dce3_01a_Core;
    global $DcMap_dce3_01a_Variant_Selectors;
    global $DcMap_dce3_01a_Semantic_Records;
    global $DcMap_dce3_01a_Mathematics;
    global $DcMap_dce3_01a_Whitespace_and_Punctuation;
    global $Dc_to_Base64;
    $counter = 14;
    $txt = '';
    $state = 'Core';
    $varAppend = 'DcMap_dce3_01a_';
    while ($counter < strlen($hex)) {
        $action_last = $action;
        log_add('<br><font color="red">' . (($counter - 14) / 2 + 1) . ': </font><br>State: ' . $state . '<br>Hex position: ' . substr($hex, $counter, 2) . '<br>Dc ID: ' . $DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))] . '<br>Appending: ');
        $data_array_name = $varAppend . $state;
        $data_array = ${$data_array_name};
        switch ($state) {
            case 'Core':
                if (substr($DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))], 0, 1) == '>') {
                    //Switch states
                    $state = substr($DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))], 1, strlen($DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))]) - 1);
                    $append = '';
                    $action = 'State switch out of Core) <br>';
                    break;
                } else {
                    $append = $DcMap_dce3_01a_Core[strtoupper(substr($hex, $counter, 2))] . ',';
                    $action = 'Append from Core) <br>';
                }
                break;
            case 'Variant_Selectors':
                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                    $state = 'Core';
                    $append = '';
                    $action = 'State switch out of Variant_Selectors) <br>';
                } else {
                    $append = $DcMap_dce3_01a_Variant_Selectors[strtoupper(substr($hex, $counter, 2))] . ',';
                    $action = 'Append from Variant_Selectors) <br>';
                }
                break;
            case 'Semantic_Records':
                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                    $state = 'Core';
                    $append = '';
                    $action = 'State switch out of Semantic_Records) <br>';
                } else {
                    $append = $DcMap_dce3_01a_Semantic_Records[strtoupper(substr($hex, $counter, 2))] . ',';
                    $action = 'Append from Semantic_Records) <br>';
                }
                break;
            case 'Mathematics':
                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                    $state = 'Core';
                    $append = '';
                    $action = 'State switch out of Mathematics) <br>';
                } else {
                    $append = $DcMap_dce3_01a_Mathematics[strtoupper(substr($hex, $counter, 2))] . ',';
                    $action = 'Append from Mathematics) . <br>';
                }
                break;
            case 'Whitespace_and_Punctuation':
                if (substr($hex, $counter, 2) == 'fd' || substr($hex, $counter, 2) == 'fe') {
                    $state = 'Core';
                    $append = '';
                    $action = 'State switch out of Whitespace_and_Punctuation) <br>';
                } else {
                    $append = $DcMap_dce3_01a_Whitespace_and_Punctuation[strtoupper(substr($hex, $counter, 2))] . ',';
                    $action = 'Append from Whitespace_and_Punctuation) <br>';
                }
                break;
        }
        if (substr($hex, $counter, 4) == 'fd03') {
            $action = '<br>Halting. <br>';
            break;
        }
        $txt = $txt . $append;
        $counter = $counter + 2;
        log_add(rtrim($append, ',') . '<br>Action: ' . substr($action, 0, strlen($action) - 6) . '<br><font color="green">→ ' . $txt . '</font><br><br>');
    }
    log_add(rtrim($append, ',') . '<br>Action: ' . substr($action, 4, strlen($action) - 5) . '<br><br><br><br>');
    $txt = substr($txt, 3, strlen($txt) - 4);
    return $txt;
}
        log_add('<br><span style="background-color:magenta;"><strong>Beginning fifth step: Convert the data to the chosen output format, and return a value.</strong></span><br>');
        $dc_to_x_function = 'convert_dc_to_' . $output_format . '_output';
        return $dc_to_x_function($dc);
        log_add('<br><span style="background-color:magenta;"><strong>Finished conversion.</strong></span><br>');
    }
}
//TESTS HAPPEN HERE
if ($tests) {
    libdce_tests();
    log_add($test_results);
}
//THIS SHOULD ALL BE AT THE END OF THE SCRIPT
if ($error_happened) {
    /*if (strpos($error_list, 'Errors were encountered during processing') !== false) {
      } else {*/
    error_add('<br><font color="red"><strong>' . $error_counter . ' errors were encountered during processing! Review the above list of error messages and/or the log for more information.</strong></font>');
    //}
}
if ($errors) {
    if ($debug) {
        log_add($error_list);
    } else {
        echo '<h2>Error list:</h2>' . $error_list;
    }
} else {
    if ($debug) {
        log_add('<h2>Error list:</h2>' . $error_list);
    } else {
    }
}
log_add('<br><span style="background-color:magenta;"><strong>Finished run at ' . date('Y.m.d-H.i.s.u.Z-I') . '.</strong></span><br><br><br><br>');