gpt4 book ai didi

python - python 中的 perl hex() 模拟

转载 作者:太空狗 更新时间:2023-10-30 02:22:40 26 4
gpt4 key购买 nike

python 中的 perl hex() 模拟如何?
我有下一个 perl 代码:

my $Lon = substr($Hexline,16,8);
say $output_f "Lon: " . hex($Lon) . "";

$Hexline 的格式为“6a48f82d8e828ce82b82...”

我在python上试了一下

Lon = int(Hexline[16:24], 16)
f.write('lon = %s' % str(Lon)+'\n')

是吗?

编辑:在 perl 的情况下,hex() 给我一个十进制值。

最佳答案

是的,要将十六进制字符串转换为整数,您可以使用 int(hex_str, 16)

请注意,在您的 write 方法调用中:

  • 您不需要连接两个字符串来添加换行符,您可以直接将其添加到格式化字符串中。
  • 要打印整数,您应该使用 %d 而不是 %s
  • 您实际上不需要调用 str 将整数转换为字符串。

因此,写入调用可以写成:

f.write('lon = %d\n' % Lon)

或者,您也可以使用 format这样:

f.write('lon = {0}\n'.format(Lon))

关于python - python 中的 perl hex() 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9257343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com