转换字节流为数字数据类型 [英] Converting byte-stream into numeric data-type

查看:195
本文介绍了转换字节流为数字数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个字节流中,我认识一个64位值(64位随机数)的位置。该字节顺序是小端。由于PHP的整数数据类型仅限于32位(至少在32位操作系统上)我将如何转换成字节序列到一个PHP数字再presentation(浮动就足够了,我认为)?

  $ serverChallenge = SUBSTR($字节,24,8);
// $ serverChallenge现在包含字节序列
其中//我知道,这是一个64位的值
 

解决方案

刚刚抬头的$ C $下 Zend_Crypt_Math_BigInteger_Bcmath Zend_Crypt_Math_BigInteger_Gmp 它处理这个​​问题:

使用BCmath(大型)

这是本质上张贴的<一个解决方案href="http://stackoverflow.com/questions/726092/converting-byte-stream-into-numeric-data-type/726323#726323">Chad桦木。

 公共静态函数bc_binaryToInteger($操作数)
{
    $结果='0';
    而(strlen的($操作数)){
        $ ORD = ORD(SUBSTR($操作数,0,1));
        $结果= bcadd(bcmul($结果,256),$ ORD);
        $操作数= SUBSTR($操作数,1);
    }
    返回$结果;
}
 

使用GMP(大型)

同度算法 - 只是不同的函数名

 公共静态函数gmp_binaryToInteger($操作数)
{
    $结果='0';
    而(strlen的($操作数)){
        $ ORD = ORD(SUBSTR($操作数,0,1));
        $结果= gmp_add(gmp_mul($结果,256),$ ORD);
        $操作数= SUBSTR($操作数,1);
    }
    返回gmp_strval($结果);
}
 

更改度算法使用豆蔻-endian字节顺序很简单:只需读取端的二进制数据来启动:

使用BCmath(豆蔻尾数)

 公共静态函数bc_binaryToInteger($操作数)
{
    //只是扭转binray数据
    $操作数= strrev($操作数);
    $结果='0';
    而(strlen的($操作数)){
        $ ORD = ORD(SUBSTR($操作数,0,1));
        $结果= bcadd(bcmul($结果,256),$ ORD);
        $操作数= SUBSTR($操作数,1);
    }
    返回$结果;
}
 

使用GMP(豆蔻尾数)

 公共静态函数gmp_binaryToInteger($操作数)
{
    //只是扭转binray数据
    $操作数= strrev($操作数);
    $结果='0';
    而(strlen的($操作数)){
        $ ORD = ORD(SUBSTR($操作数,0,1));
        $结果= gmp_add(gmp_mul($结果,256),$ ORD);
        $操作数= SUBSTR($操作数,1);
    }
    返回gmp_strval($结果);
}
 

Let's say I have a byte-stream in which I know the location of a 64-bit value (a 64-bit nonce). The byte-order is Little-Endian. As PHP's integer data-type is limited to 32-bit (at least on 32-bit operating systems) how would I convert the byte-sequence into a PHP numeric representation (float would be sufficient I think)?

$serverChallenge = substr($bytes, 24, 8);
// $serverChallenge now contains the byte-sequence 
// of which I know that it's a 64-bit value

解决方案

Just looked up the code for Zend_Crypt_Math_BigInteger_Bcmath and Zend_Crypt_Math_BigInteger_Gmp which deals with this problem:

Using BCmath (Big-Endian)

This is essentially the solution posted by Chad Birch.

public static function bc_binaryToInteger($operand)
{
    $result = '0';
    while (strlen($operand)) {
        $ord = ord(substr($operand, 0, 1));
        $result = bcadd(bcmul($result, 256), $ord);
        $operand = substr($operand, 1);
    }
    return $result;
}

Using GMP (Big-Endian)

Same algorithem - just different function names.

public static function gmp_binaryToInteger($operand)
{
    $result = '0';
    while (strlen($operand)) {
        $ord = ord(substr($operand, 0, 1));
        $result = gmp_add(gmp_mul($result, 256), $ord);
        $operand = substr($operand, 1);
    }
    return gmp_strval($result);
}

Changing the algorithem to use Litte-Endian byte-order is quite simple: just read the binary data from end to start:

Using BCmath (Litte-Endian)

public static function bc_binaryToInteger($operand)
{
    // Just reverse the binray data
    $operand = strrev($operand);
    $result = '0';
    while (strlen($operand)) {
        $ord = ord(substr($operand, 0, 1));
        $result = bcadd(bcmul($result, 256), $ord);
        $operand = substr($operand, 1);
    }
    return $result;
}

Using GMP (Litte-Endian)

public static function gmp_binaryToInteger($operand)
{
    // Just reverse the binray data
    $operand = strrev($operand);
    $result = '0';
    while (strlen($operand)) {
        $ord = ord(substr($operand, 0, 1));
        $result = gmp_add(gmp_mul($result, 256), $ord);
        $operand = substr($operand, 1);
    }
    return gmp_strval($result);
}

这篇关于转换字节流为数字数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆