2011-03-30 80 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