最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - parseInt() - String to Integer and back in PHP - Stack Overflow

programmeradmin7浏览0评论

Im trying to reverse engineering some Javascript code and recode it in PHP. There is a String which is "converted" with parseInt(String, 36) to an Integer. I need a posibility to convert the integer back to the secret String in PHP, without knowing the secret string.

secretCode = "0vo8fz4kvy03";
decode = parseInt(secretCode, 36).toString();
console.log(decode); //= 115802171408044510

Im trying to reverse engineering some Javascript code and recode it in PHP. There is a String which is "converted" with parseInt(String, 36) to an Integer. I need a posibility to convert the integer back to the secret String in PHP, without knowing the secret string.

secretCode = "0vo8fz4kvy03";
decode = parseInt(secretCode, 36).toString();
console.log(decode); //= 115802171408044510

How i can do that in PHP? 115802171408044510 back to 0vo8fz4kvy03

This interger contains some informations:

decode="115802171408044510";
storeID = decode.substr(0, 4); // 1158
posID = decode.substr(12, 2); // 04
orderID = decode.substr(14, 2); // 45
day = decode.substr(6, 2); // 17
month = decode.substr(4, 2); // 02
hour = decode.substr(8, 2); // 14
minutes = decode.substr(10, 2); // 08

I would like to edit this values above and convert this back to a "secretCode" String. They are doing exactly this somehow server-side.

Share Improve this question edited Jun 1, 2019 at 16:04 Kevin asked Jun 1, 2019 at 15:43 KevinKevin 231 silver badge5 bronze badges 2
  • you may have a look to this question, for gerater numbers than are safe integer numbers. why does this question have a javascript tag? – Nina Scholz Commented Jun 1, 2019 at 15:55
  • Correct me if I am wrong but 115802171408044510 = vo8fz4kvxzy and 0vo8fz4kvy03 = 115802171408044515 – Dharman Commented Jun 1, 2019 at 16:09
Add a ment  | 

6 Answers 6

Reset to default 3

You can't. The number overflows the maximum safe integer size, therefore it is impossible to reverse this process (as the number gets rounded to the next safe integer). Therefore the code is broken.

To convert your javascript encoded number string from base 10 to base 36:

$decode = "115802171408044510";
echo base_convert ( $decode , 10 , 36 );

You can try.

decode.toString(36)

You could take BigInt and convert large values to decimal or back to string.

function convertFrom(value, radix) {
    return [...value.toString()]
        .reduce((r, v) => r * BigInt(radix) + BigInt(parseInt(v, radix)), 0n);
}

function convertTo(value, radix) {
    var result = '',
        r = BigInt(radix);
     
    do {
        result = (value % r).toString(36) + result;
        value = value / r;
    } while (value);
    return result;
}

var secretCode = "0vo8fz4kvy03",
    value = convertFrom(secretCode, 36),
    encode = convertTo(value, 36);

console.log(value.toString());
console.log(encode.toString());

The Number.prototype.toString() method receives an optional argument, which lets you set the base of the result.

So all you need to do is (+decode).toString(36).

secretCode = "0vo8fz4kvy03";
decode = parseInt(secretCode, 36).toString();
console.log(decode); //= 115802171408044510
console.log((+decode).toString(36));

Notice that I'm converting your decode to a number. This is needed because you converted the result of parseInt directly to a string, so you never actually hold the number.

The intval function in PHP is the closest to parseInt in Javascript including the support for radix parameter:

$int = intval('9', 36);

In Javascript, parseInt returns NaN for inputs that can not be parsed to integer; but in PHP, intval returns 0.

Look for the details about intval function in php docs.

发布评论

评论列表(0)

  1. 暂无评论