简体   繁体   中英

How can I read a binary string in C#?

There is a php script which sends a binary string to the client application:

$binary_string = pack('i',count($result_matrix));   
foreach ($result_matrix as $row)
{
    foreach ($row as $cell)
    {
        $binary_string .= pack('d',$cell);
    }
}
echo $binary_string;

Silverlight application receives $binary_string via POST protocol. How can I parse this binary string?

Or maybe there is a better way to send matrix from PHP to Silverlight?

Maybe the elementary way is to send XML data? The reason is that on Silverlight side you have not only to unpack binary data, that packed with php function, but also have a knowlege how data sructure packed in php script represented in binary data.

If you will use HEX format for packing than use somethong like this to unpack data:

static byte[] UnpackHex(string hexvalue)
{
        if (hexvalue.Length % 2 != 0)
                hexvalue = "0" + hexvalue;
        int len = hexvalue.Length / 2;
        byte[] bytes = new byte[len];
        for(int i = 0; i < len; i++)
        {
                string byteString = hexvalue.Substring(2 * i, 2);
                bytes[i] = Convert.ToByte(byteString, 16);
        }
        return bytes;
}

如果以System.IO.Stream类型获取数据,则可以直接使用read方法。

In the System.IO namespace you can also use the BinaryReader class. See documentation at this location BinaryReader

I think this should help. If you need more help, perhaps you should also provide some code from your silverlight side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM