js中将 整数转成字符,,将unicode 编码后的字符还原出来的方法。
一、将整数转成字符:
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("D\u0000\u0000\u0000X\u0000\u0000\u0000L\u0000\u0000\u00000\u0000\u0000\u00002\u0000\u0000\u00000\u0000\u0000\u00004\u0000\u0000\u00000\u0000\u0000\u0000F\u0000\u0000\u00001\u0000\u0000\u00001\u0000\u0000\u00000\u0000\u0000\u00000\u0000\u0000\u00001\u0000\u0000\u00009\u0000\u0000\u0000\u0000\u0000\u0000\u0000")
//结果: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("\r\n------------\r\n"); 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;} |

浙公网安备 33010602011771号