【问题标题】:xor operator working diferent in PHP and Visual Basicxor 运算符在 PHP 和 Visual Basic 中的工作方式不同
【发布时间】:2019-01-15 08:24:53
【问题描述】:

我在使用 xor 运算符时遇到了一些问题。

我有一个 Visual Basic 应用程序,它具有这一行的功能:

numeroCaracter = Asc(password.Substring(contador, 1)) Xor Asc(CadenaEncriptacion.Substring(contador, 1))

password是函数接收的字符串,CadenaEncriptacion就是这个常量:

Private Const CadenaEncriptacion As String = "eNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaYeNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaYeNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaYeNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaY"

我需要将函数翻译成 PHP,我用这种方式翻译了那行:

$numeroCaracter = ord(substr($password, $contador, 1)) xor ord(substr($CadenaEncriptacion, $contador, 1));

PHP 中的 ord 函数和 vb 中的 Asc 函数在两种语言中给出相同的值,但 numeroCaracter 在 PHP 和 VB 中具有不同的值,通过 XOR 运算符...

在 php 中,numeroCaracer 始终是每个字符的 ord 值,在 vb 中,asc 函数给我其他值。

谢谢!

【问题讨论】:

标签: php vba vb6


【解决方案1】:

php 中,xor 的优先级低于 =,您的代码被解释为:

($numeroCaracter = ord(substr($password, $contador, 1))) xor ord(substr($CadenaEncriptacion, $contador, 1));

所以,$numeroCaracter 的值是 ord(substr($password, $contador, 1))。添加括号或使用^ 运算符代替xor

$numeroCaracter = ord(substr($password, $contador, 1)) ^ ord(substr($CadenaEncriptacion, $contador, 1));

【讨论】:

  • 我认为它们是同义词......这对我有用,非常感谢@u_mulder!
猜你喜欢
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
相关资源
最近更新 更多