2012-01-28 7 views
-2

誰かがこのコードで何が起こっているのかを説明できるので、私はPythonで書くことができます。私はPythonで書かれたコードを変換する方法

import hashlib 
import socket 
import struct 
class blommy(object): 
    def __init__(self): 
     self.bitarray= [0]*2048 

    def hashes(self,ip): 
     #convert decimal dotted quad string to long integer" 
     intip= struct.unpack('>L',socket.inet_aton(ip))[0] #>converting stringt to int 
     index = [0, 1] 
     hbyte = hashlib.sha1(intip) # # #sha1 doesnt accept int ? what needs to be done? 
     index[0] = ord(hbyte[0])| ord(hbyte[1])<< 8 
     index[1] = ord(hbyte[2])| ord(hbyte[3])<< 8 
     # how do i shift the bits? 
以下のコメントを参照してくださいPythonのためにそれを変換しようとしていますまさにこの部分にそう後で仕様が書かれた同じcはBep33ビットトレントDHTのためのPythonのブルームフィルタに

//fixed parameters 
k = 2 

m = 256*8 

//the filter 
byte[m/8] bloom ## 

function insertIP(byte[] ip) { 

byte[20] hash = sha1(ip) 

int index1 = hash[0] | hash[1] << 8 # how to in python? 
int index2 = hash[2] | hash[3] << 8 

// truncate index to m (11 bits required) 
index1 %= m ## ? 
index2 %= m ## ? 

// set bits at index1 and index2 
bloom[index1/8] |= 0x01 << index1 % 8 ## ?? 
bloom[index2/8] |= 0x01 << index2 % 8 ## ?? 
} 

// insert IP 192.168.1.1 into the filter: 
    insertIP(byte[4] {192,168,1,1}) 

を実施しますに従う必要があり

+2

C#タグとJavaScriptタグは何ですか? – alexn

+0

@alexn probbaly元のコードはC#ではなく、Cです。配列型の構文(実際のC#ではありませんが) – wRAR

+0

元のコードがc#、cなどにあっても問題はありません。 – Shazib

答えて

2

sha1はPython 2.xで通常の文字列を受け入れます。 socket.inet_aton(ip)から直接個々のバイトが必要な場合があります(アルゴリズムが正しいかどうかわからない場合)

ビットシフトについては、Pythonの構文は同じですが、括弧を追加する必要があります実行命令について疑問を呈している。

また、C charはintとして動作できますが、Pythonではord()関数とchr()関数を使用して明示的に変換する必要があります。

%=|=のようなものも、Pythonで動作します。

関連する問題