gmp_import


gmp apg

IMPORT a GMP number from a binary string.





$word_size = 1 is the default, mainly used in conjunction with the $flags parameter.

$data must be multiple of the length of the $word_size.



<?php

GMP gmp_import 
string $data
                                  
int $word_size 1
                                  
int $flags GMP_MSW_FIRST GMP_NATIVE_ENDIAN )


where,

$data The binary STRING to be imported

$word_size 
The number of bytes in each chunk of binary data

$flags 
To control how the binary string will be imported 

?>
 

$data


The binary string to be imported.



$word_size


The number of bytes in each chunk of binary data.



$flags


To control how the binary string will be imported.



RELATED FLAGS
CONTANT NAMEVALUE
GMP_ROUND_ZERO0
GMP_ROUND_PLUSINF1
GMP_ROUND_MINUSINF2
GMP_MPIR_VERSION3.0.0
GMP_VERSION6.0.0
GMP_MSW_FIRST1
GMP_LSW_FIRST2
GMP_LITTLE_ENDIAN4
GMP_BIG_ENDIAN8
GMP_NATIVE_ENDIAN16
ed48

  1 EXERCISE   

<?php

$b01 
'10111011010011';

$i01 gmp_import($b01);

echo 
'gmp_import( ' $b01 ' ) = ' $i01;

?> 

  2 EXERCISE   

<?php

$b02 
'10111011010011';

$wv02 = [ 12781428 ];

foreach(
$wv02 as $w02)
{

$i02 gmp_import($b02 $w02 );

echo 
'gmp_import( ' $b02 ', ' 
                                 
$w02 ') =<br><br>= ' 
                                 
$i02 '<br><br><br>';

}

?> 

  3 EXERCISE   

<?php

$b03 
'aaBBHHdd';

$wv03 = [ 124];

$op03 = [ GMP_LSW_FIRST
               
GMP_MSW_FIRST,
               
GMP_LITTLE_ENDIAN
               
GMP_NATIVE_ENDIAN
               
GMP_BIG_ENDIAN,
               
GMP_MSW_FIRST GMP_LITTLE_ENDIAN,
               
GMP_MSW_FIRST GMP_BIG_ENDIAN,
               
GMP_MSW_FIRST GMP_NATIVE_ENDIAN,
               
GMP_LSW_FIRST GMP_LITTLE_ENDIAN,
               
GMP_LSW_FIRST GMP_BIG_ENDIAN,
               
GMP_LSW_FIRST |GMP_NATIVE_ENDIAN ];

foreach(
$wv03 as $w03)
{

    foreach(
$op03 as $o03)
    {

$i03 gmp_import($b03 $w03$o03 );

echo 
'gmp_import( ' $b03 ', ' 
                                 
$w03 ', ' 
                                 
$o03 ') =<br><br>= ' 
                                 
$i03 '<br><br><br>';
    }
}

?> 

  4 EXERCISE   

<?php

$b04 
'-Alea Jacta est!';

$wv04 = [ 124];

$op04 = [ GMP_LSW_FIRST
               
GMP_MSW_FIRST,
               
GMP_LITTLE_ENDIAN
               
GMP_NATIVE_ENDIAN
               
GMP_BIG_ENDIAN,
               
GMP_MSW_FIRST GMP_LITTLE_ENDIAN,
               
GMP_MSW_FIRST GMP_BIG_ENDIAN,
               
GMP_MSW_FIRST GMP_NATIVE_ENDIAN,
               
GMP_LSW_FIRST GMP_LITTLE_ENDIAN,
               
GMP_LSW_FIRST GMP_BIG_ENDIAN,
               
GMP_LSW_FIRST |GMP_NATIVE_ENDIAN ];

foreach(
$wv04 as $w04)
{

    foreach(
$op04 as $o04)
    {

$i04 gmp_import($b04 $w04$o04 );

echo 
'gmp_import( ' $b04 ', ' 
                                  
$w04 ', ' 
                                  
$o04 ') =<br><br>= ' 
                                  
$i04 '<br><br><br>';
    }
}

?> 

  5 EXERCISE   

<?php

// Tests taken from GMPs own test suite.

// format is [expected, size, options, input]
$import = [
    [
'0',1,GMP_BIG_ENDIAN,''],
    [
'12345678',1,GMP_BIG_ENDIAN,'12345678'],
    [
'12345678',4,GMP_BIG_ENDIAN,'12345678'],
    [
'12345678',4,GMP_LSW_FIRST GMP_BIG_ENDIAN,'12345678'],
    [
'12345678',1,GMP_LSW_FIRST GMP_LITTLE_ENDIAN,'78563412'],
    [
'12345678',4,GMP_LITTLE_ENDIAN,'78563412'],
    [
'12345678',4,GMP_LSW_FIRST GMP_LITTLE_ENDIAN,'78563412'],
    [
'123456789abc',2,GMP_BIG_ENDIAN,'123456789abc'],
    [
'123456789abc',2,GMP_LSW_FIRST GMP_BIG_ENDIAN,'9abc56781234'],
    [
'123456789abc',2,GMP_LITTLE_ENDIAN,'34127856bc9a'],
    [
'123456789abc',2,GMP_LSW_FIRST GMP_LITTLE_ENDIAN,'bc9a78563412'],
    [
'112233445566778899aabbcc',4,GMP_BIG_ENDIAN,'112233445566778899aabbcc'],
    [
'112233445566778899aabbcc',4,GMP_LSW_FIRST GMP_BIG_ENDIAN,'99aabbcc5566778811223344'],
    [
'112233445566778899aabbcc',4,GMP_LITTLE_ENDIAN,'4433221188776655ccbbaa99'],
    [
'112233445566778899aabbcc',4,GMP_LSW_FIRST GMP_LITTLE_ENDIAN,'ccbbaa998877665544332211'],
    [
'100120023003400450056006700780089009a00ab00bc00c',8,GMP_BIG_ENDIAN,'100120023003400450056006700780089009a00ab00bc00c'],
    [
'100120023003400450056006700780089009a00ab00bc00c',8,GMP_LSW_FIRST GMP_BIG_ENDIAN,'9009a00ab00bc00c50056006700780081001200230034004'],
    [
'100120023003400450056006700780089009a00ab00bc00c',8,GMP_LITTLE_ENDIAN,'044003300220011008800770066005500cc00bb00aa00990'],
    [
'100120023003400450056006700780089009a00ab00bc00c',8,GMP_LSW_FIRST GMP_LITTLE_ENDIAN,'0cc00bb00aa0099008800770066005500440033002200110']
];

$passed true;
foreach (
$import as $k => $test) {
    
$gmp gmp_import(hex2bin($test[3]), $test[1], $test[2]);
    if (
$gmp instanceof GMP) {
        
$result gmp_strval($gmp16);
        if (
$result !== $test[0]) {
            echo 
"$k: '$result' !== '{$test[0]}'<br>";
            
$passed false;
        }
    } else {
        
$type gettype($gmp);
        echo 
"$k$type !== '{$test[0]}'<br>";
    }
}

var_dump($passed);

// Invalid word sizes
try {
    
var_dump(gmp_import('a', -1));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_import('a'0));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

// Invalid data lengths
try {
    
var_dump(gmp_import('a'2));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_import('aa'3));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_import(str_repeat('a'100), 64));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

// Invalid options
try {
    
var_dump(gmp_import('a'1GMP_MSW_FIRST GMP_LSW_FIRST));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
try {
    
var_dump(gmp_import('a'1GMP_BIG_ENDIAN GMP_LITTLE_ENDIAN));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}
?>