fromCharCode vs chr

echo off

set "fn=%*"

set php=d:/www/php5/php.exe

cls

echo.

%php% %fn%

pause>null

====================

JS 默认编码是 UTF-16 ,PHP 中对数据的处理编码是 UTF-8 ,对 ASCII 无所谓,但对汉字就影响大了,所以必须做转码

<?php

$encKey="12";

$str="1234abcASDF/?廣告服務    风《》<>您好";

echo 'PHP:'.$str."\r\n<br>";

$str=utf82Unicode($str);

$ex=encrypt_string($str);

function encrypt_string($input)

{

    global $encKey;

    $line = "";

    for($i = 0; $i < strlen($input); $i++) {

        $line .= chr(ord($input[$i]) + $encKey);//ord()返回字符的 ASCII 值。十进制

    }                                           //chr()从指定的 ASCII 值返回字符。

    return $line;

}

function deccrypt_string($input)

{

    global $encKey;

    $line = "";

    for($i = 0; $i < strlen($input); $i++){

        $line .= chr(ord($input[$i]) - $encKey);

    }

    return $line;

}

function Unicode2Utf8($str){

preg_match_all("/(\d{2,5})/", $str,$a);

$a = $a[0];

$utf = '';

foreach ($a as $dec){

if ($dec < 128) {

$utf .= chr($dec);

} else if ($dec < 2048) {

$utf .= chr(192 + (($dec - ($dec % 64)) / 64));

$utf .= chr(128 + ($dec % 64));

} else {

$utf .= chr(224 + (($dec - ($dec % 4096)) / 4096));

$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));

$utf .= chr(128 + ($dec % 64));

}

}

return $utf;

}

function utf82Unicode($str=""){

$unicode = array();

$values = array();

$lookingFor = 1;

for ($i = 0; $i < strlen($str); $i++ ) {

$thisValue = ord($str[$i]);

if ($thisValue < 128) {

$unicode[] = $thisValue;

} else {

if ( count( $values ) == 0 ) {

$lookingFor = ( $thisValue < 224 ) ? 2 : 3;

}

$values[] = $thisValue;

if ( count( $values ) == $lookingFor ) {

$number = ( $lookingFor == 3 ) ?

( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):

( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );

$unicode[] = $number;

$values = array();

$lookingFor = 1;

}

}

}

$return = '';

foreach($unicode as $val){

$return .= '&#'.$val.';';

}

return $return;

}

?>

JS:

<script>

var e="<? echo $ex; ?>";

var s= decrypt_string(e);

document.write(s);

function decrypt_string(estr){

var key = 12;

var str="";

var n = estr.length;

var i;

for(i=0; i<n; i++) {

dstr =estr.charCodeAt(i) - key;

str+=String.fromCharCode(dstr);

}

return str;

}

</script>

fromCharCode vs chr的更多相关文章

  1. 常用ASCII CHR碼對照

    因為開發需求,把對照表留下來一下. Chr(0) Null Chr(29) 分组符 Chr(38) & Chr(48) 0 Chr(8) 退格 Chr(30) 記錄分離符號 Chr(39) ‘ ...

  2. Oracle中的CHR()函数与ASCII()函数

    工作中经常会处理一些因特殊字符而导致的错误,如上周我就遇到了因为换行符和回车符导致的数据上报的错误,这种错误比较难以发现,通常是由于用户的输入习惯导致的,有可能数据极少,就那么几行错误从而导致整个数据 ...

  3. 类型转换bin()、chr()、ord() 、int()、float()、str()、repr()、bytes()、tuple(s )、 list(s )   、unichr(x ) 、 ord(x )  、 hex(x )  、          type()数据类型查询

    1.bin() 将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer: 参数x:整数或者包含__index__()方法切返回值 ...

  4. javascript中String的fromCharCode()方法

    前几天遇到一个bug,后端的模板引擎在输出形如: <div title="111 aaa">内容</div> 这样的内容时,无法输出' '空格,所以只能用' ...

  5. Python chr() ord() unichr()

    chr()函数用一个范围在range(256)内的(就是0-255)整数作参数,返回一个对应的字符. unichr()跟它一样,只不过返回的是Unicode字符,这个从Python 2.0才加入的un ...

  6. php中的字符串常用函数(四) ord() 获得字符的ascii码 chr()获取ascii码对应的字符

    ord('a');//=>97 返回小写a 的ascii码值97 chr(97);//=>a 返回ascii码表上的97对应的 小写a

  7. python 中chr(),unichr(),ord()的用法

    chr()根据整数返回对应的字符,也就是讲ascii转换为字符 unichr()将整数返回成unicode字符 ord()将字符转换成ascii码

  8. python chr() unichr() ord()

    了解一下python chr(),unichr(),ord()函数的用法. 参考链接: http://crazier9527.iteye.com/blog/411001 chr() 输入参数(取值范围 ...

  9. python_way,day4 内置函数(callable,chr,随机验证码,ord),装饰器

    python_way,day4 1.内置函数 - 下 制作一个随机验证码 2.装饰器 1.内置函数 - 下 callable() #对象能否被调用 chr() #10进制数字对应的ascii码表中的内 ...

随机推荐

  1. Ubuntu 安装软件和centos 对比命令

    之前都是使用Redhat 或者Centos 等rpm的linux  ,开始使用ubuntu 很不习惯 1.  安装命令Centos  : yum install httpd               ...

  2. Entity Framework介绍和DBFirst开发方式

    一.ORM概念  什么是ORM? 对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术.简单来说,就是将关系型数 ...

  3. ASE加、解密

    AES已经变成目前对称加密中最流行算法之一:AES可以使用128.192.和256位密钥,并且用128位分组加密和解密数据. /** * 加密 * * @param content 需要加密的内容 * ...

  4. BZOJ 3544 treap (set)

    我只是想找个treap的练习题-- 每回找到lower_bound 就好啦 //By SiriusRen #include <cstdio> #include <cstring> ...

  5. Android setBackgroundResource和setBackgroundDrawable和用法

    两个方法的效果是一样,只是区别于效率! playBtn.setBackgroundResource(R.drawable.pause_selecor); 从上面可以看出来是从资源文件中获取drawab ...

  6. Oracle 删除重复数据的几种方法

    去重 第一种:distinct create table tmp_t3 as select distinct * from t3; drop table t3; alter table tmp_t2 ...

  7. installp 操作

    installp  软件安装和升级工具     1.查看某个已应用更可被提交或拒绝的文件集) installp -s   2. 应用更新TCP/IP软件( /usr/sys/inst.images ) ...

  8. window下搭建Vue.Js开发环境

    一.安装node.js.https://nodejs.org/en/download/ 最新包会自动安装npm 二.安装完node之后,npm包含的很多依赖包是部署在国外的,在天朝,大家都知道下载速度 ...

  9. 【2017 Multi-University Training Contest - Team 1 1002】Balala Power!

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6034 [Description] 给你n个字符串; 每个字符串都仅由小写字母组成; 然后,你可以把 ...

  10. Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob

    Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob 在正式的项目开发中,单client不能满足我们的需求,须要实现client与服务端的连接. 而在编写Android服务端代码 ...