zoukankan      html  css  js  c++  java
  • 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("Du0000u0000u0000Xu0000u0000u0000Lu0000u0000u00000u0000u0000u00002u0000u0000u00000u0000u0000u00004u0000u0000u00000u0000u0000u0000Fu0000u0000u00001u0000u0000u00001u0000u0000u00000u0000u0000u00000u0000u0000u00001u0000u0000u00009u0000u0000u0000u0000u0000u0000u0000")

    //结果: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(" ------------ ");
     
            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;
    }
  • 相关阅读:
    完善例题3.2的日期类mydate
    杨辉三角形
    求100以内的素数
    九九乘法表
    实现计算机界面
    完善3.2例题
    杨辉三角法
    素数程序
    九九乘法表
    杨辉三角
  • 原文地址:https://www.cnblogs.com/oxspirt/p/4793658.html
Copyright © 2011-2022 走看看