Perl - 读取探测值后的字节交换
Perl - byte swaping after reading the probe values
我需要 Perl 方面的帮助。也许对你来说是一个容易回答的问题......
从 AD Converter 我在 debian wheezy 上得到类似 0X43b7 的值。
我用 i2cget 命令读取这个值
$EC = `sudo i2cget -y 1 0x4a 0x00 w` ;
我得到的是一个十六进制值 0x34c2。使用过的微控制器对这个值有点像小端,我需要将高值字节与低值字节交换。我得到了提示,但是在 python 中......我不知道如何处理这个问题。在 Perl 中是否有针对此 Python 行的简单表达式?
assert line.startswith("0x")
word = int(line[2:], 16)
yield struct.unpack(">H", struct.pack("<H", word))[0]
我从来没有在 Perl 中交换字节,也不太清楚如何翻译它。
如果输入是字符串 0x34c2
并且你想得到字节 43 2c
(或字符串“432c”),你可以使用
my $EC = "0x34c2";
my $output = pack 'h4', substr $EC, 2; # --> \x43\x2c
my $string = unpack 'H4', $output; # --> "432c"
如果只想交换字符串中的 "bytes",可以使用替换
my $EC = "0x34c2";
(my $output = $EC) =~ s/(..)(..)$//;
或substr:
my $EC = "0x34c2";
my $output = $EC;
substr $output, 4, 0, substr $output, 2, 2, q();
你不清楚你想要什么输出,但我通过 运行 你的代码片段确定了以下内容:
对于输入,您有一个字符串,例如 0x34c2
。
对于输出,你想要数字 4971610 = C23416.
您可以使用许多不同的方法。
die("assert") if substr($s, 0, 2) ne "0x";
my $n = unpack('S<', pack('H*', substr($s, 2))); # If it's a LE uint16_t
-or-
my $n = unpack('s<', pack('H*', substr($s, 2))); # If it's a LE int16_t
或者,
my $n = unpack('S<', pack('S>', hex($s))); # If it's a LE uint16_t
-or-
my $n = unpack('s<', pack('s>', hex($s))); # If it's a LE int16_t
两种解决方案都适用于小端和大端平台。
从评论来看,下一行似乎应该是:
my $ECdec = $n/10;
print "Electric Conductivity $ECdec µS/m\n";
$ python <<'EOS'
import struct
line = "0x34c2"
assert line.startswith("0x")
word = int(line[2:], 16)
word = struct.unpack(">H", struct.pack("<H", word))[0]
print word
EOS
49716
$ perl -e'
use feature qw( say );
my $s = "0x34c2";
die("assert") if substr($s, 0, 2) ne "0x";
my $word = unpack("S<", pack("H*", substr($s, 2)));
say $word;
'
49716
我需要 Perl 方面的帮助。也许对你来说是一个容易回答的问题...... 从 AD Converter 我在 debian wheezy 上得到类似 0X43b7 的值。 我用 i2cget 命令读取这个值
$EC = `sudo i2cget -y 1 0x4a 0x00 w` ;
我得到的是一个十六进制值 0x34c2。使用过的微控制器对这个值有点像小端,我需要将高值字节与低值字节交换。我得到了提示,但是在 python 中......我不知道如何处理这个问题。在 Perl 中是否有针对此 Python 行的简单表达式?
assert line.startswith("0x")
word = int(line[2:], 16)
yield struct.unpack(">H", struct.pack("<H", word))[0]
我从来没有在 Perl 中交换字节,也不太清楚如何翻译它。
如果输入是字符串 0x34c2
并且你想得到字节 43 2c
(或字符串“432c”),你可以使用
my $EC = "0x34c2";
my $output = pack 'h4', substr $EC, 2; # --> \x43\x2c
my $string = unpack 'H4', $output; # --> "432c"
如果只想交换字符串中的 "bytes",可以使用替换
my $EC = "0x34c2";
(my $output = $EC) =~ s/(..)(..)$//;
或substr:
my $EC = "0x34c2";
my $output = $EC;
substr $output, 4, 0, substr $output, 2, 2, q();
你不清楚你想要什么输出,但我通过 运行 你的代码片段确定了以下内容:
对于输入,您有一个字符串,例如 0x34c2
。
对于输出,你想要数字 4971610 = C23416.
您可以使用许多不同的方法。
die("assert") if substr($s, 0, 2) ne "0x";
my $n = unpack('S<', pack('H*', substr($s, 2))); # If it's a LE uint16_t
-or-
my $n = unpack('s<', pack('H*', substr($s, 2))); # If it's a LE int16_t
或者,
my $n = unpack('S<', pack('S>', hex($s))); # If it's a LE uint16_t
-or-
my $n = unpack('s<', pack('s>', hex($s))); # If it's a LE int16_t
两种解决方案都适用于小端和大端平台。
从评论来看,下一行似乎应该是:
my $ECdec = $n/10;
print "Electric Conductivity $ECdec µS/m\n";
$ python <<'EOS'
import struct
line = "0x34c2"
assert line.startswith("0x")
word = int(line[2:], 16)
word = struct.unpack(">H", struct.pack("<H", word))[0]
print word
EOS
49716
$ perl -e'
use feature qw( say );
my $s = "0x34c2";
die("assert") if substr($s, 0, 2) ne "0x";
my $word = unpack("S<", pack("H*", substr($s, 2)));
say $word;
'
49716