0

I need if possible port this function to c++:

static unsigned char bux_code[3] = {0xFC, 0xCF, 0xAB};

void bux_convert(char* buf, int size)
{
        int n = 0;
        // ----
        for (n=0;n<size;n++)
        {
                buf[n]^=bux_code[n%3];
        }
}

What i have done:

$bux_code = array("\xFC", "\xCF", "\xAB");

function bux_convert($string)
{
    $size = strlen($string);

    for ($n = 0; $n < $size; $n++) {
        $string[$n] ^= $bux_code[$n % 3];
    }

    return $string;
}

var_dump ( bux_convert("£Ì•½Ï¼«ü") );

But i got this error: Cannot use assign-op operators with overloaded objects nor string

4 Answers 4

1

In PHP you can't always address an offset of a string like $mystring[5] or $mystring[$k] and then treat the resulting character as a numeric value in every respect. Here's a way you could get around it: turn the string into an array at the start of bux_convert(), using str_split(), and then turn it back into a string at the end, using implode().

Also, $bux_code is a global variable and won't be visible inside the function. Either move it inside the function or declare it as a global.

function bux_convert($string)
{
    $bux_code = array(0xFC, 0xCF, 0xAB);

    $size = strlen($string);
    $string = array_map('ord', str_split($string));

    for ($n = 0; $n < $size; $n++) {
        $string[$n] ^= $bux_code[$n % 3];
    }

    return implode('', array_map('chr', $string));
}

var_dump ( bux_convert("£Ì•½Ï¼«ü") );
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Thank you (and all others)!
0

try this.

 $bux_code = array(hexdec("FC"), hexdec("CF"), hexdec("AB"));

Comments

0

The error is complaining about the "^=" assignment operator.

So, try doing what the error message suggests, and rewrite the assignment without using the assign-op operator:

$string[$n] = $string[$n] ^ $bux_code[$n % 3];

1 Comment

Ok, working now. But function returning "2212212212". In c++ it returning normal word
0

You need to explicitly port globals to the function, so on the first line of your function put the line:

global $bux_code;

Now, the variable is not found, is undeclared, and therefor xor cannot operator on it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.