To convert an integer to a character:
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)
Results: dxl02040f110019
Second, the data transmitted by JSON, Unicode encoded characters into ordinary characters:
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;
}
Call
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 ")
Results: dxl02040f110019
Here's a excerpt:
-
The decimal number (ASCII character a) is stored in the file and then read out.
-
Admin 2012-10-23
- 2
The main research is 97 in memory, and the byte order problem in the file. There is also the difference between int char. Implemented in C: Vim 1.c?
12345678910111213 |
#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;
}
|
GCC 1.chexdump-c a.out[[email protected] ~]# hexdump-c 1.data
00000000, |a...|, XX
00000004 can be seen in reverse storage, 0x61 to decimal is 97, that is, ASCII character A. Then we'll read this value from the file: Vim 2.c?
123456789101112131415161718192021222324 |
#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>
|
GCC 2.c &&./a.out output:
[Email protected] ~]#./a.out
61 00 00 00
------------
97 Summary: 1. The file faithfully saves the data in memory, and how to write it as you read it. 2. The sequence of bytes in the Intel x86 Litter-endian int memory is reversed (char is a positive order). Network byte order, file byte order is a positive sequence. The PHP implementation will save 97 files:?
1234 |
<?php $s = pack( "L*" , 97); file_put_contents ( ‘./1.data‘ , $s ); ?> |
What if I want to store a bunch of them at once? Pack ("l*", Array (97, 98, 99, 100)); This is wrong! The result is that the array () converted to a value of 1eval () is not good for the cache. Implements an array of support:?
123456789101112131415 |
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
;
}
|
JS converts integers into characters, and restores Unicode-encoded characters out of the method.