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 us开发者_运维技巧e assign-op operators with overloaded objects nor string
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("£Ì•½Ï¼«ü") );
try this.
$bux_code = array(hexdec("FC"), hexdec("CF"), hexdec("AB"));
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];
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.
精彩评论