2015-09-18 36 views
2

我想知道如何將md5哈希轉換爲大整數,以便我可以將模運算符應用到它。轉換md5哈希到bigint在R

我創建使用digest庫哈希:

h <- digest("luca", algo="md5", ascii=TRUE, raw=TRUE) 
> h 
[1] 18 0e 41 2e 42 db 5a 8c 45 3c 8a 81 c5 90 4e 5b 

我現在想h轉換成一個大的整數,並能夠模運算符(%%)適用於它。

我該怎麼做?

回答

2

使用Rmpfr庫,以下工作:

# Get a hex string of the MD5 hash: 
h = digest("luca", algo="md5", ascii = TRUE, raw = FALSE) 
result = mpfr(h, base = 16) 
result 
# 1 'mpfr' number of precision 128 bits 
# [1] 31975486076668374554448903959384968795 

result %% 1024 
# 1 'mpfr' number of precision 128 bits 
# [1] 603 

要安裝Rmpfr,一個需要安裝的依賴,在GNU MPFR庫。查看評論以獲取更多信息。

+1

它工作正常!非常感謝! 對於那些有發行人安裝Rmpfr的人,你需要庫libmpfr-dev。 在像Ubuntu系統,你可以安裝它: 命令和apt-get安裝libmpfr-dev的 ,然後在有R安裝Rmpfr: install.packages( 「Rmpfr」) – lucacerone

+1

也可以跳過粘貼步驟,通過詢問摘要來產生字符串: h < - digest(「luca」,algo =「md5」,ascii = TRUE,raw = FALSE) – lucacerone

+1

@lucacerone酷。既然兩者都是有價值的評論,我會直接將它們放入其他用戶的答案中,以便更容易看到它們。 –