duandong2562 2012-09-25 13:57
浏览 79

Javascript函数到Php

I need to encode a password to hexadecimal in javscript and then decode it in php. I have a function that does the two things but in javascript. I tried to transform it to php but there are some javascript native functions that i dont know what is the php equivalent.

The function to encode (Javascript)

function encodeToHex(str){
    var r="";
    var e=str.length;
    var c=0;
    var h;
    while(c<e){
        h=str.charCodeAt(c++).toString(16);
        while(h.length<3) h="0"+h;
        r+=h;
    }
    return r;
}

The function to decode (Javascript)

function decodeFromHex(str){
    var r="";
    var e=str.length;
    var s;
    while(e>=0){
        s=e-3;
        r=String.fromCharCode("0x"+str.substring(s,e))+r;
        e=s;
    }
    return r;
}

The function to decode (PHP) << This is what im trying to achieve

function decodeFromHex($str){
$r="";
$e=str.length;
$s;
while($e>=0){
        $s=$e-3;
        $r=String.fromCharCode("0x"+str.substring($s,$e))+$r;
        $e=$s;
    }
    return $r;
}
  • 写回答

2条回答 默认 最新

  • dsj8000 2012-09-25 14:02
    关注

    You need hexdec and dechex functions.

    By the way, you get string length in php via strlen().

    On a last note, client side encoding is quite pointless, as it's easy to sniff and decode your data - use SSL over HTTPS if you want to be sure.

    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用