首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在PHP中获取字符串的字节值?

在PHP中,可以使用ord()函数来获取字符串中某个字符的ASCII值,也可以使用unpack()函数来获取字符串的字节值。

示例代码:

代码语言:php
复制
$str = "Hello, world!";
$len = strlen($str);

for ($i = 0; $i < $len; $i++) {
    $byte = ord($str[$i]);
    echo "Byte value of character " . $i . " is: " . $byte . "\n";
}

$byteArray = unpack('C*', $str);
print_r($byteArray);

输出结果:

代码语言:txt
复制
Byte value of character 0 is: 72
Byte value of character 1 is: 101
Byte value of character 2 is: 108
Byte value of character 3 is: 108
Byte value of character 4 is: 111
Byte value of character 5 is: 44
Byte value of character 6 is: 32
Byte value of character 7 is: 119
Byte value of character 8 is: 111
Byte value of character 9 is: 114
Byte value of character 10 is: 108
Byte value of character 11 is: 100
Byte value of character 12 is: 33
Array
(
    [1] => 72
    [2] => 101
    [3] => 108
    [4] => 108
    [5] => 111
    [6] => 44
    [7] => 32
    [8] => 119
    [9] => 111
    [10] => 114
    [11] => 108
    [12] => 100
    [13] => 33
)

在这个示例中,我们首先使用strlen()函数获取字符串的长度,然后使用ord()函数遍历字符串中的每个字符,并输出它们的ASCII值。最后,我们使用unpack()函数将字符串转换为字节数组,并输出结果。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

5分40秒

如何使用ArcScript中的格式化器

7分1秒

086.go的map遍历

2分25秒

090.sync.Map的Swap方法

1分3秒

医院PACS影像信息管理系统源码带三维重建

6分9秒

054.go创建error的四种方式

2分29秒

基于实时模型强化学习的无人机自主导航

16分8秒

人工智能新途-用路由器集群模仿神经元集群

领券