Skip to content

Instantly share code, notes, and snippets.

@lenivene
Created February 5, 2017 11:38
Show Gist options
  • Save lenivene/be3f9ab1eac94303f311b7fee538ba6f to your computer and use it in GitHub Desktop.
Save lenivene/be3f9ab1eac94303f311b7fee538ba6f to your computer and use it in GitHub Desktop.
Encode 256 bytes and 10 bytes
<?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