2011-09-20 52 views
-1

我知道有人會爲此付出很大的代價,但是在這裏。我試圖將一些C代碼轉換爲Java,並且仍然在學習Java。我有一個非常困難的時間搞清楚如何做這個轉換和學習Java的事情。任何幫助或指示去哪裏將不勝感激。C到Java的轉換

for(d=alldevs; d; d=d->next) 
    { 
     printf("%d. %s", ++i, d->name); 
     if (d->description) 
      printf(" (%s)\n", d->description); 
     else 
      printf(" (No description available)\n"); 
    } 

    if(i==0) 
    { 
     printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); 
     return -1; 
    } 

    printf("Enter the interface number (1-%d):",i); 
    scanf("%d", &inum); 

    if(inum < 1 || inum > i) 
    { 
     printf("\nInterface number out of range.\n"); 
     /* Free the device list */ 
     pcap_freealldevs(alldevs); 
     return -1; 
    } 

    /* Jump to the selected adapter */ 
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); 
+5

哪個部分,**明確**,你有麻煩嗎?沒有人會給你翻譯的代碼... –

+0

指針#1。學習C,如果你已經做了,去指針#2。指針#2。學習Java,如果您已經這樣做,請參閱指針#3。指針#3。轉換你的代碼,瞧! – Marcelo

+0

我在學習Java的過程中遇到困難的一部分。我不是程序員,完全是自學的。我在這裏尋求一些幫助。提前致謝。 – PGFDBUG

回答

1

A轉換祭

public class Item { 

    private String name; 

    private String description; 

    public Item(String name, String description) { 
    this.name = name; 
    this.description = description; 
    } 

    public String getName() { 
    return this.name; 
    } 

    public String getDescription() { 
    return this.description; 
    } 

} 

(defined elsewhere) 
List<Item> items; 

而且現在的代碼

int index = 1; 
for (Item item: items) { 
    System.out.print(index + ". " + item.getName()); 
    if (item.getDescription() != null) { 
    System.out.println(" (" + item.getDescription() + ")"); 
    } else { 
    System.out.println"((No Description Available)"); 
    } 
    index++; 
}  

List<NetworkInterface> nets = Collections.list(NetworkInterface.getNetworkInterfaces()); 
if (nets.size() == 0) { 
    System.out.println(); 
    System.out.println("No interfaces found! Make sure WinPcap is installed."); 
    return; 
} 

System.out.print("Enter the interface number(1-" + nets.size() + "):"); 
Byte[] input = new byte[100]; 
System.in.read(input); 
String inString = new String(input); 
int interfaceIndex = Integer.getInteger(inString); 

if (interfaceIndex < 1 || interfaceindex > nets.size()) { 
    System.out.println(); 
    System.out.println("Interface number out of range."); 
    // freeing items is done by dereferencing 
    nets = null; 
    return; 
} 

// jump to the selected adapter 
NetworkInterface selectedInterface = nets.get(interfaceIndex); 

請注意,你將不得不做程序的其餘部分的「真實」的轉換;即使用Java編寫PCap解決方案,或編寫JNI(Java)接口來調用PCap庫例程。

+0

謝謝你給我一些工作。我不指望爲我完成這項工作只需要幫助,並指出可以使用的所有信息到哪裏去。再次感謝。 – PGFDBUG

+0

@PGFDBUG,好吧,我通常不會爲某人做所有的工作;但是,您所遇到的部件並不是真正的PCap部件。 Java處理列表的方式不同,並且您可能通過將C列表轉換爲Java列表獲得更多收益,而不是完全模擬C列表。 –

0

WinPcap將始終需要本機編碼。您可以使用JNI將一些代碼(例如選擇接口部分)轉換爲java,但您永遠無法完全轉換它。 (你不能直接從java調用原生dll的)

我真的不能給你關於JNI的例子,但是當找到JNI和教程或類似的東西時,有足夠的地方可以找到:)。

0

Java沒有指針,所以像「d-> next」這樣的結構不會在Java中映射1:1。

你必須弄清楚如何創建一個對應於你的「d」的Java對象。其餘的看起來像簡單的迭代和調用System.out。

0

你是什麼意思轉換
您是否正在嘗試爲Java編寫一個等效的C程序?它是一個現有的項目,還是您先用C編寫了一個程序,現在您想用Java編寫相同的程序?
在任何情況下,您都應該對兩種語言都有好的掌握,因爲它們在編寫程序時的不同之處在於C是一種命令式/過程式語言,而Java則是一種面向對象的語言。僅這一點就意味着由於語言語義(和編程模式,而與語言無關,也被不同地應用),程序設計'風格'會有所不同。

P.S .:我假設你是初學者,如果我是正確的,不能證明你的選票是正確的。

+0

謝謝我是初學者我有一個體面的C掌握,並試圖瞭解Java。一些我沒有很好地處理的事情就是這些。 – PGFDBUG

+0

沒問題;):) – Kounavi