Home >Backend Development >PHP Tutorial >中文怎么转换为bytes

中文怎么转换为bytes

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:33:321195browse

例如“广东”  转化成byte[]={[-27, -71, -65, -28, -72, -100]} 

用一个现成的函数还是要稍微编个函数


回复讨论(解决方案)

var b = Encoding.UTF8.GetBytes("广东");Console.WriteLine(string.Join(",", b));
229,185,191,228,184,156

Byte 是无符号整型,如果你一定要负数的话就每项减 256

求直接的php代码,临时用,以后再仔细研究

$s = "广东";$s = iconv('gbk', 'utf-8', $s);$r = array_map('ord', str_split($s));print_r($r);
Array(    [0] => 229    [1] => 185    [2] => 191    [3] => 228    [4] => 184    [5] => 156)

$s = "广东";$s = iconv('gbk', 'utf-8', $s);$r = unpack('C*', $s);print_r($r);
Array(    [1] => 229    [2] => 185    [3] => 191    [4] => 228    [5] => 184    [6] => 156)
$s = "广东";$s = iconv('gbk', 'utf-8', $s);$r = unpack('C*', $s);print_r($r);
Array(    [1] => -27    [2] => -71    [3] => -65    [4] => -28    [5] => -72    [6] => -100)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn