Created
February 5, 2017 11:38
-
-
Save lenivene/be3f9ab1eac94303f311b7fee538ba6f to your computer and use it in GitHub Desktop.
Encode 256 bytes and 10 bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function b256_to_b10_encode( $string, $encoding = "UTF-8" ) { | |
bcscale( 0 ); | |
$result = "0"; | |
for ($i = mb_strlen( $string, $encoding )-1; $i >= 0; $i--) { | |
$result = bcadd( $result, bcmul( ord( $string[$i] ), bcpow( 256, $i ) ) ); | |
} | |
return $result; | |
} | |
function b10_to_b256_encode( $string ) { | |
bcscale(0); | |
$result = ""; | |
$n = $string; | |
do { | |
$remainder = bcmod( $n, 256 ); | |
$n = bcdiv( $n, 256 ); | |
$result .= chr( $remainder ); | |
} while ($n > 0); | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment