0

Given i have these codes of javascripts. I already tried to convert it to PHP but it only returns #000000

function getuidcolor(str) {

    var hash = 0;
    for (var i = 0; i < str.length; i++) {
        hash = str.charCodeAt(i) + ((hash << 5) - hash);
    }

    var colour = '#';

    for (var i = 0; i < 3; i++) {
        var value = (hash >> (i * 8)) & 0xFF;
        colour += ('00' + value.toString(16)).substr(-2);
    }
    return colour;
}

What i have so far:

function string2color($str) {

    $hash = 0;
    for ($i = 0; $i < strlen($str); $i++) {
        $hash = $str[$i].(($hash << 5) - $hash);
    }

    $colour = '#';

    for ($i = 0; $i < 3; $i++) {

        $value = ($hash >> ($i * 8)) & '0xFF';

        $hex = '00' . base_convert($value, 10, 16);

        $colour .= substr($hex, -2);

    }

    return $colour;
}


echo string2color('dfgdgfdgfdgfdgfdg');

My PHP so far works without any errors but it does not really return what it supposed to.

johndavedecano
  • 522
  • 5
  • 11

3 Answers3

0
function string2color($str) {

    $hash = 0;
    for ($i = 0; $i < strlen($str); $i++) {
        // It was:
        // $hash = $str[$i].(($hash << 5) - $hash);
        // What's wrong:
        // 1) You need char code not char
        // 2) You need operate number not string
        // So =>
        $hash = ord($str[$i]) + (($hash << 5) - $hash);
    }

    $colour = '#';

    for ($i = 0; $i < 3; $i++) {
        // It was:
        // $value = ($hash >> ($i * 8)) & '0xFF';
        // What's wrong:
        // You need operate number not string
        // So =>
        $value = ($hash >> ($i * 8)) & 255;

        // It was:
        // $hex = '00' . base_convert($value, 10, 16);
        // What's wrong:
        // 1) You need convert decimal to hex
        // So =>
        $hex = dechex($value);

        $colour .= substr($hex, -2);

    }

    return $colour;
}
stdob--
  • 28,222
  • 5
  • 58
  • 73
0

You have some mistake in your code,such as, you used function charCodeAt to unicode str but not in php.

I use a similar function in php to get the same result in js. here is my code,hope to help you:

<?php

function string2color($str) {

$hash = 0;
for ($i = 0; $i < strlen($str); $i++) {

    $encdoe_str = charCodeAt($str,$i);

    $hash = $encdoe_str+(($hash << 5) - $hash);
}

$colour = '#';

for ($i = 0; $i < 3; $i++) {

    $value = ($hash >> ($i * 8)) & 0xFF;

    $hex = '00' . base_convert($value, 10, 16);

    $colour .= substr($hex, -2);

}

return $colour;
}
echo string2color('dfgdgfdgfdgfdgfdg');

function charCodeAt($str, $index){
    $char = mb_substr($str, $index, 1, 'UTF-8');


if (mb_check_encoding($char, 'UTF-8'))
{
    $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
    return hexdec(bin2hex($ret));
}


  else
    {
        return null;
    }
}

?>
<script type="text/javascript">
    function getuidcolor(str) {

        var hash = 0;
        for (var i = 0; i < str.length; i++) {
            hash = str.charCodeAt(i) + ((hash << 5) - hash);
        }

        var colour = '#';

        for (var i = 0; i < 3; i++) {
            var value = (hash >> (i * 8)) & 0xFF;
            colour += ('00' + value.toString(16)).substr(-2);
        }
        return colour;
    }
    console.log(getuidcolor('dfgdgfdgfdgfdgfdg'));
</script>
0

This one is working. For both 32-bit (PHP_INT_SIZE == 4) and 64-bit integers (PHP_INT_SIZE == 8):

function string2color($str) {

    $hash = 0;

    for ($i = 0; $i < strlen($str); $i++) {

        $encdoe_str = charCodeAt($str,$i);

        $hash = $encdoe_str+(_32bitleftshift($hash, 5) - $hash);
    }

    $colour = '#';

    for ($i = 0; $i < 3; $i++) {

        $value = _32bitrightshift($hash, ($i * 8)) & 0xFF;

        $hex = '00' . base_convert($value, 10, 16);

        $colour .= substr($hex, -2);

    }

    return $colour;
}

function charCodeAt($str, $index){

    $char = mb_substr($str, $index, 1, 'UTF-8');

    if (mb_check_encoding($char, 'UTF-8'))
    {
        $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
        return hexdec(bin2hex($ret));
    } else {
        return null;
    }
}

function _32bitleftshift($number, $steps) 
{ 
    $result = 0; 

    if (strlen(decbin($number)) + $steps <= 31) { 

        $result = $number << $steps; 
    } else { 
     // shift left 
     $binary = decbin($number).str_repeat("0", $steps); 
     // get the last 32 digits 
     $binary = substr($binary, strlen($binary) - 32); 
     // is the leftmost digit 1, i.e., is it a negative number? 
     if ($binary{0} == "1") { 
      // get the last 31 digits 
      $binary = substr($binary, strlen($binary) - 31); 
      // get the 1's complement 
      $binary_1s_complement = ""; 
      for ($i = 0; $i <= 30; $i++) { 
        $binary_1s_complement .= ($binary{$i} == "0"? "1" : "0"); 
      } 
      // get the 2's complement 
      $binary_2s_complement = decbin(bindec($binary_1s_complement) + 1); 
      $result = -1 * bindec($binary_2s_complement); 
     } else { 
      $result = bindec($binary); 
     } 
    } 
    return $result; 
} 

function _32bitrightshift($x, $c) {

    $x = intval ($x); // Because 13.5 >> 0 returns 13. We follow.

    $nmaxBits = PHP_INT_SIZE * 8;
    $c %= $nmaxBits;

    if ($c)
        return $x >> $c & ~ (-1 << $nmaxBits - $c);
    else
        return $x;
}

echo string2color('b11b92c6-cdb8-488b-925c-0a0651b1b5b3');
johndavedecano
  • 522
  • 5
  • 11