如何在php中将字符转换为7位偶校验

发布于 2024-08-14 17:14:26 字数 36 浏览 11 评论 0原文

我想将一个字符转换为 7 位偶校验。您能建议我如何实施吗?

I want to convert a Character to a 7 bit even parity. Can you please suggest me, how to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-08-21 17:14:26

太糟糕了,您不能使用 x86 JPO 指令(如果奇偶校验为奇数则跳转);-)

根据您想要处理的数据量,如果您首先设置翻译表,可能会比逐个字符检查/处理更快。

$map = array();
for($char=0; $char<128; $char++) {
  $parity = 0;
  for($bit=0; $bit<8; $bit++) {
    if($char & (1<<$bit)) {
      $parity ^= 128;
    }
  }
  $map[chr($char)] = chr($char|$parity);
}

(您可能想彻底测试这段代码,但我没有)
然后使用 strtr() 将 ascii7 转换为 ascii7-evenbit。

$input = 'mary had a little lamb'; // all characters must be within the ascii7 range
$evenbit = strtr($input, $map);
// test output
for($i=0; $i<strlen($evenbit); $i++) {
  printf("%08s\n", decbin(ord($evenbit[$i])));
}

Too bad you can't use the x86 JPO instruction (Jump if Parity Odd) ;-)

Depending on the amount of data you want to handle it might be faster if you first set up a translation table than to check/handle character by character.

$map = array();
for($char=0; $char<128; $char++) {
  $parity = 0;
  for($bit=0; $bit<8; $bit++) {
    if($char & (1<<$bit)) {
      $parity ^= 128;
    }
  }
  $map[chr($char)] = chr($char|$parity);
}

(you might want to test this code thoroughly, I haven't)
and then use strtr() to translate from ascii7 to ascii7-evenbit.

$input = 'mary had a little lamb'; // all characters must be within the ascii7 range
$evenbit = strtr($input, $map);
// test output
for($i=0; $i<strlen($evenbit); $i++) {
  printf("%08s\n", decbin(ord($evenbit[$i])));
}
面如桃花 2024-08-21 17:14:26

冒着被否决的风险:

function to7BitEven($byte) {
   if( $byte > 0x7f ) return 0;
   $binary = decbin($byte);
   $one = 0;
   $ret = 0;

   for($x=0;$x<strlen($binary);$x++) {
      if( $binary[$x] == '1' ) $one++;
   }

   if( $one % 2 != 0 ) $ret = "1";
   else $ret = "0";

   for($x=0;$x<(7-strlen($binary));$x++)
      $ret .= "0";

   $ret .= $binary;
   return $ret;
}

echo to7BitEven(0x7f) . "\n";
echo to7BitEven(0x41) . "\n";
echo to7BitEven(0x3a) . "\n";
echo to7BitEven(0x3b) . "\n";

这将计算 1,并在第一位添加额外的 1(如果是奇数)或 0(如果是偶数)。然后将其他 7 位复制到返回值中。这将为您留下 7 位的字符串表示形式,以及字节的偶校验。

对于那些比我更有经验的人来说,这看起来正确吗?输出示例:

11111111

01000001

00111010

10111011

At the risk of downvotes:

function to7BitEven($byte) {
   if( $byte > 0x7f ) return 0;
   $binary = decbin($byte);
   $one = 0;
   $ret = 0;

   for($x=0;$x<strlen($binary);$x++) {
      if( $binary[$x] == '1' ) $one++;
   }

   if( $one % 2 != 0 ) $ret = "1";
   else $ret = "0";

   for($x=0;$x<(7-strlen($binary));$x++)
      $ret .= "0";

   $ret .= $binary;
   return $ret;
}

echo to7BitEven(0x7f) . "\n";
echo to7BitEven(0x41) . "\n";
echo to7BitEven(0x3a) . "\n";
echo to7BitEven(0x3b) . "\n";

This will count the 1's, and add an extra 1 (if odd) or a 0 (if even) to the first bit. Then copy the other 7 bits into the return. This will leave you with a string representation of the 7 bit, even parity of the byte.

To those more experienced then I, does this look right? Example output:

11111111

01000001

00111010

10111011

葬心 2024-08-21 17:14:26

这是一个 C 版本:

uint8_t even_parity (uint8_t b)
{
    return b ^ ((3459840 >> ((b ^ (b >> 4)) & 15)) & 128);
}

我确信翻译成 PHP 会很容易,但我会避免让自己尴尬。

受到这个 Bit Twiddling Hack 的启发。

Here's a C version:

uint8_t even_parity (uint8_t b)
{
    return b ^ ((3459840 >> ((b ^ (b >> 4)) & 15)) & 128);
}

I'm sure translation to PHP would be easy, but I'll avoid embarrassing myself.

Inspired by this Bit Twiddling Hack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文