普通网友 2014-05-28 08:11
浏览 36
已采纳

遗留C#到PHP

I have recently taken over a legacy service and I'm in the process of converting it to PHP, however I have gotten stuck with the more intricate functionality of C#.

From what I see the request message length needs to be packed and then concatted with the actual message before being sent to the service.

string strReceiveBuffer;
string msg1 = string.Empty;
msg1 = msg1 + txtRqst.Text;
Int64 intMessageLength = msg1.Length;
byte byt1; byte byt2;

//The below should work assuming a 16 bit register.
byt1 = Convert.ToByte(decimal.Truncate(intMessageLength/ 256));
byt2 = Convert.ToByte(decimal.Remainder(intMessageLength, 256));
string strLength = string.Empty;
strLength = strLength 
    + Microsoft.VisualBasic.Strings.Chr(byt1).ToString() 
    + Microsoft.VisualBasic.Strings.Chr(byt2).ToString();

//Only encode the length using BigEndianCode, the message content is encoded using UTF8
byte[] byteData = Encoding.BigEndianUnicode.GetBytes(strLength)
    .Concat(Encoding.UTF8.GetBytes(msg1)).ToArray();

Is anybody able to help with the above code before I tear my hair out?

  • 写回答

2条回答 默认 最新

  • dongle2627 2014-06-03 06:32
    关注

    To anybody that has to convert C# to PHP, good luck. After struggling with variations of encoding and decoding I finally managed to translate it to PHP.

    function convertRequest($msg) {
        $length = strlen($msg);
        $byt1 = intval($length / 256);
        $byt2 = $length % 256;
        $strLength = mb_convert_encoding((string) chr($byt1) . (string) chr($byt2), 'UTF-16BE');
        $msg = mb_convert_encoding($msg, 'UTF-8');
        $data = $strLength . $msg;
        return $data;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题