php - convert ebcdic to ascii

久未见 提交于 2020-12-04 12:59:12

问题


$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;

When this code block executes, $my_var has the value ¦–¦

The test RPGLE program being called is returning wow

When the program is altered, the characters i get back are different... so i know i'm getting back values from the program. The only issue is, how to convert these characters from EBCDIC to ASCII.


edit

I have tried using the following:

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

But i get this error: "Wrong charset, conversion from" ebcdic to ascii "is not allowed"

And, "Wrong charset, conversion from" cp037 to ascii "is not allowed"


回答1:


On your CREATE PROCEDURE statement, use the CCSID that matches your language. For US English, that would be 37. Example:

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                



回答2:


This is working for us on several systems (IBM i v7r1, PHP Version 5.4.16):

$ascii_value = iconv("IBM-1047","ISO8859-1", $ebcdic_value); // EBCDIC->ASCII conversion




回答3:


Use the following PHP function:

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



回答4:


built in function EBCDIC to ASCII

I had to do this in window environment

 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;

}

?>


来源:https://stackoverflow.com/questions/10499656/php-convert-ebcdic-to-ascii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!