php - 将ebcdic转换为ascii

时间:2012-05-08 13:32:50

标签: php sql ascii ibm-midrange ebcdic

$my_var = "1";

$connection = db2_connect ( "*LOCAL", "ISCSC", "ISCSC" );

$strSql = 'CALL LMTLIB.ARTEST(?)';

$stmt = db2_prepare ( $connection, $strSql );

db2_bind_param($stmt, 1, "my_var", DB2_PARAM_INOUT, DB2_CHAR);

$rc = db2_execute ( $stmt );

echo $my_var;

执行此代码块时,$ my_var的值为|-|

正在调用的测试RPGLE程序正在返回哇

当程序被更改时,我回来的字符是不同的...所以我知道我从程序中获取值。唯一的问题是,如何将这些字符从EBCDIC转换为ASCII。


修改

我尝试过使用以下内容:

mb_convert_encoding($my_var, "ASCII", "cp037");
mb_convert_encoding($my_var, "ASCII", "EBCDIC");
iconv("cp037", "ASCII", $my_var);
iconv("EBCDIC", "ASCII", $my_var);

但是我得到了这个错误:“错误的字符集,不允许从”ebcdic“转换为”ascii“”

并且,“错误的字符集,不允许从”cp037转换为ascii“”

4 个答案:

答案 0 :(得分:2)

这对我们在几个系统上起作用(IBM i v7r1,PHP Version 5.4.16):

$ ascii_value = iconv(“IBM-1047”,“ISO8859-1”,$ ebcdic_value); // EBCDIC-> ASCII转换

答案 1 :(得分:1)

使用以下PHP函数:

$recoded = recode_string ('EBCDIC..ASCII', $my_var);

答案 2 :(得分:1)

在CREATE PROCEDURE语句中,使用与您的语言匹配的CCSID。对于美国英语,那将是37.例如:

CREATE PROCEDURE buck.phptest(         
 inout parm_inout varchar (5) ccsid 37)
LANGUAGE RPGLE                         
NOT DETERMINISTIC                      
CALLED ON NULL INPUT                   
EXTERNAL NAME buck.phptest             
PARAMETER STYLE GENERAL                

答案 3 :(得分:0)

内置函数EBCDIC到ASCII

我必须在窗口环境中执行此操作

 function ebcdic_to_ascii($ebcdic_hexstring /*expecting something like F0F1....*/)
    {
        //need to delcare it to avoid warning
$ebcd_ascii= null;
  // here come all the conversion
$ebcd_ascii["4A"] = "¢";
$ebcd_ascii["4B"] = ".";
$ebcd_ascii["4C"] = "<";
$ebcd_ascii["4D"] = "(";
$ebcd_ascii["4E"] = "+";
$ebcd_ascii["4F"] = "|";
$ebcd_ascii["5A"] = "!";
$ebcd_ascii["5B"] = "$";
$ebcd_ascii["5C"] = "*";
$ebcd_ascii["5D"] = ")";
$ebcd_ascii["5E"] = ";";
$ebcd_ascii["5F"] = "¬";
$ebcd_ascii["60"] = "-";
$ebcd_ascii["61"] = "/";
$ebcd_ascii["6A"] = "¦";
$ebcd_ascii["6B"] = ",";
$ebcd_ascii["6C"] = "%";
$ebcd_ascii["6D"] = "_";
$ebcd_ascii["6E"] = ">";
$ebcd_ascii["6F"] = "?";
$ebcd_ascii["79"] = "`";
$ebcd_ascii["7A"] = ":";
$ebcd_ascii["7B"] = "#";
$ebcd_ascii["7C"] = "@";
$ebcd_ascii["7D"] = "'";
$ebcd_ascii["7E"] = "=";
$ebcd_ascii["7F"] = " '' ";
$ebcd_ascii["81"] = "a";
$ebcd_ascii["82"] = "b";
$ebcd_ascii["83"] = "c";
$ebcd_ascii["84"] = "d";
$ebcd_ascii["85"] = "e";
$ebcd_ascii["86"] = "f";
$ebcd_ascii["87"] = "g";
$ebcd_ascii["88"] = "h";
$ebcd_ascii["89"] = "i";
$ebcd_ascii["91"] = "j";
$ebcd_ascii["92"] = "k";
$ebcd_ascii["93"] = "l";
$ebcd_ascii["94"] = "m";
$ebcd_ascii["95"] = "n";
$ebcd_ascii["96"] = "o";
$ebcd_ascii["97"] = "p";
$ebcd_ascii["98"] = "q";
$ebcd_ascii["99"] = "r";
$ebcd_ascii["A1"] = "~";
$ebcd_ascii["A2"] = "s";
$ebcd_ascii["A3"] = "t";
$ebcd_ascii["A4"] = "u";
$ebcd_ascii["A5"] = "v";
$ebcd_ascii["A6"] = "w";
$ebcd_ascii["A7"] = "x";
$ebcd_ascii["A8"] = "y";
$ebcd_ascii["A9"] = "z";
$ebcd_ascii["C0"] = "{";
$ebcd_ascii["C1"] = "A";
$ebcd_ascii["C2"] = "B";
$ebcd_ascii["C3"] = "C";
$ebcd_ascii["C4"] = "D";
$ebcd_ascii["C5"] = "E";
$ebcd_ascii["C6"] = "F";
$ebcd_ascii["C7"] = "G";
$ebcd_ascii["C7"] = "H";
$ebcd_ascii["C9"] = "I";
$ebcd_ascii["D0"] = "}";
$ebcd_ascii["D1"] = "J";
$ebcd_ascii["D2"] = "K";
$ebcd_ascii["D3"] = "L";
$ebcd_ascii["D4"] = "M";
$ebcd_ascii["D5"] = "N";
$ebcd_ascii["D6"] = "O";
$ebcd_ascii["D7"] = "P";
$ebcd_ascii["D8"] = "Q";
$ebcd_ascii["D9"] = "R";
$ebcd_ascii["E0"] = "\\";
$ebcd_ascii["E2"] = "S";
$ebcd_ascii["E3"] = "T";
$ebcd_ascii["E4"] = "U";
$ebcd_ascii["E5"] = "V";
$ebcd_ascii["E6"] = "W";
$ebcd_ascii["E7"] = "X";
$ebcd_ascii["E8"] = "Y";
$ebcd_ascii["E9"] = "Z";
$ebcd_ascii["F0"] = "0";
$ebcd_ascii["F1"] = "1";
$ebcd_ascii["F2"] = "2";
$ebcd_ascii["F3"] = "3";
$ebcd_ascii["F4"] = "4";
$ebcd_ascii["F5"] = "5";
$ebcd_ascii["F6"] = "6";
$ebcd_ascii["F7"] = "7";
$ebcd_ascii["F8"] = "8";
$ebcd_ascii["F9"] = "9";
$ebcd_ascii["FF"] = "E0";
    //end of conversion

    // loop until there is no more conversion.
    $asciiOut = "";    
    while(strlen($ebcdic_hexstring)>1)//F0F1F2F3F -> F1F2F3F
    {
        $thisEbcdic = substr($ebcdic_hexstring, 0, 2);//F0->F1
        //if(!is_null($ebcd_ascii[$thisEbcdic]))
        $asciiOut = $asciiOut.$ebcd_ascii[$thisEbcdic];//0->01
        $ebcdic_hexstring = substr($ebcdic_hexstring, 2);//F1F2F3F -> F2F3F
    }    

    return $asciiOut;

}

?>