2011-03-30 81 views
3

我想解压从C程序传递到Perl脚本通过SysV :: IPC的无符号长整型值。如何在64位Perl中解压缩(64位)unsigned long?

我们知道这个值是正确的(我做了一个测试,将相同的值发送到两个队列中,一个由Perl读取,另一个由C应用程序读取),并且所有的过期值都被正确读取(用q代替的i!使用64位整数)。

它也知道,PHP已经在错误something similar(搜索 “无符号长在64台机器”)(似乎是相似的: Pack/unpack a 64-bit int on 64-bit architecture in PHP

参数到目前为止测试:

  • ..Q(=一些值,该值是比预期更大)
  • ..L(= O)
  • ..L! (=大值)
  • ..(= 0)
  • ..l! (=大数值)
  • ..ln! (= 0)
  • ..N,..N! (= 0)

use bigint; use bignum; - 无效。

详细说明:

  • sizeof(unsigned long) = 8;
  • Data::Dumper->new([$thatstring])->Useqq(1)->Dump();沿着一些有意义的空字节很多..
  • byteorder ='12345678';

解决方案: - x4Q填充四个字节。

+0

显示Data :: Dumper-> new($ string) - > Useqq(1) - > Dump'的输出,以及您期望的数字。也许输出perl -V:byteorder – ysth 2011-03-30 15:44:17

+0

对不起,我的意思是输出'print Data :: Dumper ...' – ysth 2011-03-30 16:04:20

+0

@ysth,VAR1 =“\ 210 \ 23 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 210 \ 23 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 177 \ 0 \ 0 \ 1 \ 0 \ 0 \ 0 \ 0 \ 177 \ 0 \ 0 \ 1 \ 0 \ 0 \ 0 \ 0W \ 273 \ @ \ 37 \ 0 \ 0 \ 0 \ 0}^\ 330U \ 0 \ 0 \ 0 \ 0 \ 264 \ 13 \ 340U \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 16T \ 223M \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0" ; - 哇,太多了\ 0,检查。也许'我的$ string'声明不适合返回类型。 – 2011-03-30 16:11:33

回答

1

该溶液很简单:加入x4Q跳过之前实际值的四个字节;需要更直观地考虑填充/对齐。

3

开箱使用Qthe template作品开箱,如果你有64位的Perl:

The TEMPLATE is a sequence of characters that give the order 
and type of values, as follows: 

... 

q A signed quad (64-bit) value. 
Q An unsigned quad value. 
     (Quads are available only if your system supports 64-bit 
     integer values _and_ if Perl has been compiled to support those. 
     Causes a fatal error otherwise.) 

对于更强大的解决方案,解压价值为8字节字符串,并使用Math::Int64模块其转换成一个整数:

use Math::Int64 qw(:native_if_available int64); 

... 

$string_value = unpack("A8", $longint_from_the_C_program); 

# one of these two functions will work, depending on your system's endian-ness 
$int_value = Math::Int64::native_to_int64($string_value); 
$int_value = Math::Int64::net_to_int64($string_value); 
+0

既不能正常工作;源值= 563026057,在辅助C应用中解压缩相同,native_to_int64 = 2418178501610831872,net_to_int64 = 2300088097 – 2011-03-30 16:04:36

+0

* _to_uint64也给出了错误的结果 – 2011-03-30 16:45:41