【问题标题】:equivalent in php for byte array in .NET.NET 中字节数组的 php 等效项
【发布时间】:2014-08-15 14:35:02
【问题描述】:

当我们使用 web 服务时,我们需要将一个参数作为 base64 二进制传递,但我必须将图像转换为字节数组。我用了base64_encode,但是没用。

该 Web 服务是基于 .NET 构建的,该参数被视为字节数组。所以我想知道how we could create such a thing in PHP which is equivalent to byte array in .NET 目前我正在使用这个,

   $byte_array = file_get_contents('C:\xampp\htdocs\my_site\RGB.jpg');
     $image = base64_encode($byte_array);

但不幸的是,这不起作用...... 提前谢谢..

【问题讨论】:

    标签: php .net web-services bytearray soap-client


    【解决方案1】:
    $string = file_get_contents('C:\xampp\htdocs\my_site\RGB.jpg');
    
    # 011000010110001001100011
    
    # https://stackoverflow.com/questions/6382738/convert-string-to-binary-then-back-again-using-php
    $value = unpack('H*', $string);
    $binary = str_pad(base_convert($value[1], 16, 2),strlen($string)*8,'0',STR_PAD_LEFT);
    
    var_dump($binary);
    
    $bytes = str_split($binary,8);
    
    var_dump($bytes);
    

    [1]Convert string to binary then back again using PHP

    [2]http://www.php.net/str_pad

    [3]http://www.php.net/str_split

    演示: http://sandbox.onlinephpfunctions.com/code/29a6666e63dbd0f85bd51db59e469e7f88c48825

    【讨论】:

      猜你喜欢
      • 2013-07-07
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 2010-09-20
      相关资源
      最近更新 更多