一、将整数转成字符:
String.fromCharCode(17496>>8,17496&0xFF,19504>>8,19504&0xFF,12848>>8,12848&0xFF,13360>>8,13360&0xFF,17969>>8,17969&0xFF,12592>>8,12592&0xFF,12337>>8,12337&0xFF,14592>>8,14592&0xFF)
//结果:DXL02040F110019
二、将json传过来的数据, unicode 编码的字符转成普通字符:
function ascii2native(asciicode) {
asciicode = asciicode.split("\u");
var nativeValue = asciicode[0];
for (var i = 1; i < asciicode.length; i++) {
var code = asciicode[i];
nativeValue += String.fromCharCode(parseInt("0x" + code.substring(0, 4)));
if (code.length > 4) {
nativeValue += code.substring(4, code.length);
}
}
return nativeValue;
}
//调用
ascii2native("Du0000u0000u0000Xu0000u0000u0000Lu0000u0000u00000u0000u0000u00002u0000u0000u00000u0000u0000u00004u0000u0000u00000u0000u0000u0000Fu0000u0000u00001u0000u0000u00001u0000u0000u00000u0000u0000u00000u0000u0000u00001u0000u0000u00009u0000u0000u0000u0000u0000u0000u0000")
//结果:DXL02040F110019
下面是摘抄的:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <stdio.h> int main() { FILE *fp; int a[1] = {97}; // 这个数组只存放一个数:97 fp = fopen ( "./1.data" , "wb" ); fwrite (a, 4, 1, fp); fclose (fp); return 0; } |
00000000 61 00 00 00 |a...|
00000004
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <stdio.h> int main() { FILE *fp; int a[4] = {97, 98, 99, 100}; char c[16]; fp = fopen ( "./1.data" , "rb" ); fread (c, 16, 1, fp); fclose (fp); int i; for (i=0; i<4; i++) { printf ( "%02x " , c[i]); } printf ( "
------------
" ); for (i=0; i<1; i++) { printf ( "%d" , a[i]); } return 0; }<br> |
61 00 00 00
------------
97
1
2
3
4
|
<?php $s = pack( "L*" , 97); file_put_contents ( './1.data' , $s ); ?> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function int_to_string( $arr ) { $s = '' ; foreach ( $arr as $v ) { $a = sprintf( '%08x' , $v ); $b = '' ; // int 在内存中为逆序存放 $b .= chr ( base_convert ( substr ( $a , 6, 2), 16, 10)); $b .= chr ( base_convert ( substr ( $a , 4, 2), 16, 10)); $b .= chr ( base_convert ( substr ( $a , 2, 2), 16, 10)); $b .= chr ( base_convert ( substr ( $a , 0, 2), 16, 10)); //echo $a; $s .= $b ; } return $s ; } |