php php中字符串到字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/885597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:13:00  来源:igfitidea点击:

String to byte array in php

phpbytearray

提问by Splendid

How can I get the byte array from some string which can contain numbers, letters and so on? If you are familiar with Java, I am looking for the same functionality of the getBytes() method.

如何从一些可以包含数字、字母等的字符串中获取字节数组?如果您熟悉 Java,我正在寻找与 getBytes() 方法相同的功能。

I tried a snippet like this one:

我尝试了这样的片段:

for($i = 0; $i < strlen($msg); $i++){
    $data.=ord($msg[$i]);
        //or $data[]=ord($msg[]); 
}

but without success, so any kind of help will be appreciated.

但没有成功,所以任何形式的帮助将不胜感激。

PS: Why do I need this at all!? Well, I need to send a byte array via fputs() to a server written in Java...

PS:我为什么需要这个!?好吧,我需要通过 fputs() 将字节数组发送到用 Java 编写的服务器...

回答by Bronek

@Sparr is right, but I guess you expected byte array like byte[]in C#. It's the same solution as Sparr did but instead of HEX you expected intpresentation (range from 0 to 255) of each char. You can do as follows:

@Sparr 是对的,但我猜你期望像byte[]C# 中的字节数组。这是相同的解决方案SPARR没有,但不是诅咒你预期int表现(从0到255范围内的每一个)char。您可以执行以下操作:

$byte_array = unpack('C*', 'The quick fox jumped over the lazy brown dog');
var_dump($byte_array);  // $byte_array should be int[] which can be converted
                        // to byte[] in C# since values are range of 0 - 255

By using var_dumpyou can see that elements are int(not string).

通过使用,var_dump您可以看到元素是int( notstring)。

   array(44) {  [1]=>  int(84)  [2]=>  int(104) [3]=>  int(101) [4]=>  int(32)
[5]=> int(113)  [6]=>  int(117) [7]=>  int(105) [8]=>  int(99)  [9]=>  int(107)
[10]=> int(32)  [11]=> int(102) [12]=> int(111) [13]=> int(120) [14]=> int(32)
[15]=> int(106) [16]=> int(117) [17]=> int(109) [18]=> int(112) [19]=> int(101)
[20]=> int(100) [21]=> int(32)  [22]=> int(111) [23]=> int(118) [24]=> int(101)
[25]=> int(114) [26]=> int(32)  [27]=> int(116) [28]=> int(104) [29]=> int(101)
[30]=> int(32)  [31]=> int(108) [32]=> int(97)  [33]=> int(122) [34]=> int(121)
[35]=> int(32)  [36]=> int(98)  [37]=> int(114) [38]=> int(111) [39]=> int(119)
[40]=> int(110) [41]=> int(32)  [42]=> int(100) [43]=> int(111) [44]=> int(103) }

Be careful: the output array is of 1-based index (as it was pointed out in the comment)

小心:输出数组是基于 1 的索引(正如评论中指出的那样)

回答by Sparr

print_r(unpack("H*","The quick fox jumped over the lazy brown dog"))

Array ( [1] => 54686520717569636b20666f78206a756d706564206f76657220746865206c617a792062726f776e20646f67 ) 

T = 0x54, h = 0x68, ...

T = 0x54, h = 0x68, ...

You can split the result into two-hex-character chunks if necessary.

如有必要,您可以将结果拆分为两个十六进制字符的块。

回答by karim79

You could try this:

你可以试试这个:

$in_str = 'this is a test';
$hex_ary = array();
foreach (str_split($in_str) as $chr) {
    $hex_ary[] = sprintf("%02X", ord($chr));
}
echo implode(' ',$hex_ary);

回答by soulmerge

PHP has no explicit bytetype, but its stringis already the equivalent of Java's byte array. You can safely write fputs($connection, "The quick brown fox …"). The only thing you must be aware of is character encoding, they must be the same on both sides. Use mb_convert_encoding()when in doubt.

PHP 没有明确的byte类型,但它string已经相当于 Java 的字节数组。您可以安全地编写fputs($connection, "The quick brown fox …"). 您唯一必须注意的是字符编码,它们在两侧必须相同。如有疑问,请使用mb_convert_encoding()

回答by troelskn

In PHP, strings are bytestreams. What exactly are you trying to do?

在 PHP 中,字符串是字节流。你到底想做什么?

Re: edit

回复:编辑

Ps. Why do I need this at all!? Well I need to send via fputs() bytearray to server written in java...

附言。为什么我需要这个!?好吧,我需要通过 fputs() bytearray 发送到用 java 编写的服务器...

fputstakes a string as argument. Most likely, you just need to pass your string to it. On the Java side of things, you should decode the data in whatever encoding, you're using in php (the default is iso-8859-1).

fputs接受一个字符串作为参数。很可能,您只需要将字符串传递给它。在 Java 方面,您应该以在 php 中使用的任何编码对数据进行解码(默认值为 iso-8859-1)。

回答by Albert Liao

I found several functions defined in http://tw1.php.net/unpackare very useful.
They can covert string to byte array and vice versa.

我发现http://tw1.php.net/unpack中定义的几个函数非常有用。
他们可以将字符串转换为字节数组,反之亦然。

Take byteStr2byteArray() as an example:

以 byteStr2byteArray() 为例:

<?php
function byteStr2byteArray($s) {
    return array_slice(unpack("C*", "##代码##".$s), 1);
}

$msg = "abcdefghijk";
$byte_array = byteStr2byteArray($msg);

for($i=0;$i<count($byte_array);$i++)
{
   printf("0x%02x ", $byte_array[$i]);
}
?>