普通网友 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条)

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan