一、将整数转成字符:

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

下面是摘抄的:

将十进制数字 97 (ASCII 字符 a)存入文件,然后读出来。

admin  2012-10-23
 2
主要考察97在内存中,和文件中的字节序问题。
还有 int char 的区别。
 
用C语言实现:
 
vim 1.c
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;
 
}

gcc 1.c

hexdump -C a.out
[root@localhost ~]# hexdump -C 1.data
00000000 61 00 00 00 |a...|
00000004

可以看到是逆序存放的,0x61 转为十进制便是 97, 也就是 ascii 字符 a。

 
然后我们来从文件中读取这个值:
vim 2.c
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>

gcc 2.c && ./a.out

输出:

[root@localhost ~]# ./a.out 
61 00 00 00 
------------
97
 
总结一下:
1. 文件忠实的保存了内存中的数据,怎么读就怎么写即可。
2. intel x86 litter-endian int 内存中的字节序的为逆序(char 为正序)。网络字节序,文件字节序都为正序。
 
 
用PHP实现将 97 存入文件:
1
2
3
4
<?php
$s = pack("L*", 97);
file_put_contents('./1.data', $s);
?>
如果一次要存放一堆怎么办?
pack("L*", array(97, 98, 99, 100)); // 这样是错误的!结果是 array() 转为的数值 1
eval() 不利于 cache ,最好别用。
 
实现了一个支持数组的:
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;
}

js中将 整数转成字符,,将unicode 编码后的字符还原出来的方法。的更多相关文章

  1. Python如何将字符和Unicode编码转变

    小小总结一下,以防过几天忘记,自己的复习资料,如果能帮到大家,也是有所作用!! 1,字符转化为Unicode编码方法: ord("字符") ord("A") o ...

  2. PHP解码unicode编码的中文字符

    问题背景:晚上在抓取某网站数据,结果在数据包中发现了这么一串编码的数据:"......\u65b0\u6d6a\u5fae\u535a......www.jinyuanbao.cn" ...

  3. php正确解码javascript中通过escape编码后的字符

    js的escape如何在PHP中来解呢? 下面的这个函数可以正确的解析,网上有不少unescape的函数,但好用的不多. 这是很久以前收集的一个,不知道谁写的了,但经过测试没有问题~ function ...

  4. js中四舍五入保留两位效数,js中将Number转换成字符类型

    今天在写代码的时候遇到了点问题,特意记下,以免忘记!四舍五入方法: // num为传入的值,n为保留的小数位 function fomatFloat(num,n){ var f = parseFloa ...

  5. js中将字符串转换成json的方式

    1.eval 方式解析,实际中用的还是比较少 function evalJson(str){ var json = eval('(' + str + ')'); return json; } 2.使用 ...

  6. js中将字符串转换成json的三种方式

    1,eval方式解析,恐怕这是最早的解析方式了.如下: function strToJson(str){ var json = eval('(' + str + ')'); return json; ...

  7. C# 获取字符的Unicode编码

    using UnityEngine;using System.Collections;using System.Collections.Generic; List<); string chars ...

  8. 字符串及其操作,字符的Unicode编码

    plainText=input('message:') for c in plainText: print(chr(ord(c)-3),end='') plainText=input('message ...

  9. PHP-解码unicode编码的中文字符

    在Python中使用 "\uxxxx".decode("unicode_escape") 1. class Helper_Tool { public stati ...

随机推荐

  1. webservice简介及客户端搭建

    在环境变量中,CLASSPATH添加D:\apache-cxf-2.4.2\lib 新建CXF_HOME   D:\apache-cxf-2.4.2 在PATH中添加   D:\apache-cxf- ...

  2. 照着例子学习protobuf-python

    以下是照着python操作protobuf进行的protobuf-python的学习笔记: 首先是protobuf的下载与安装: 1 由于google被墙,所以去github上面搜索了一下protob ...

  3. python2.7学习记录之二

    一.高级特性 1.切片取前3个元素用L[0:3]表示,从索引0开始取,直到索引3为止,但不包括索引3.如果第一个索引是0可省略.前10个数 每两个取一个L[:10:2],所有数 每5个取一个L[::5 ...

  4. 时间转换(字符串转date 年月日时分秒 格式)

    /**     * 时间转换     * @param data     * @return     */    public String getValidDateStr(Date data) {  ...

  5. hdu_5750_Dertouzos(线性筛)

    题目连接:hdu_5750_Dertouzos 题意: 给你一个n,一个d,问你比n小的数中有多少个数的最大的因子为d,比如6有因子1 2 3 最大的为3 题解: 当时比赛做这题的时候没考虑常数的优化 ...

  6. Git 删除文件

    在Git中,删除也是一种修改的操作,我们验证一下,先在工作目录中添加一个新文件test.txt,并且提交: $ git statusOn branch masterUntracked files:  ...

  7. 其他信息: Error creating context 'spring.root': 未能加载文件或程序集“EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项

    详细错误情况: “System.Configuration.ConfigurationErrorsException”类型的异常在 Spring.Core.dll 中发生,但未在用户代码中进行处理 其 ...

  8. sqlQuery.list()方法返回类型

    SQLQuery sqlQuery = session.createSQLQuery(this.sql.toString()); List<Object[]> list = (List&l ...

  9. ./encrypt: error while loading shared libraries: libcrypto.so.10:

    ./encrypt: error while loading shared libraries: libcrypto.so.10:

  10. Java学习笔记之接口和抽象类

    接口(interface)1.interface创建一个接口,implements实现接口 interface jiekou{} class lie implements jiekou{}2.接口可以 ...