2016-11-30 339 views
0

使用arduino Wifi庫(型號MKR1000),我從網頁收到一個值。當然,我收到的價值是ascii,所以Arduino無法接收它並將其編入一個數字。所以基本上,而不是62(網頁上的數字),我收到「5450」(其中54是ASCII值爲6,50是ASCII值爲2)。將ASCII轉換爲int

我的代碼是

String c; 
// By setting this to String it prints out 5450 
// By setting this to int it instead prints out "hp" 

if (client.connect(server, 80)) { 
    Serial.println("connected to server"); 
    // Make a HTTP request: 
    client.println("GET /arduino/electricity.php"); 
    delayNumber = 0; 
} 


    while (client.available()) { 
    c = c + client.read(); 
    } 
//Adds 54 and then 50 to c 


Serial.print("C is: " + c); 
//Currently prints 5450 

我完全喪失。我怎樣才能讓我的變量「c」是62?

編輯:我使用這個表http://www.asciitable.com/

+0

傻到問,爲什麼6的DEC值爲54和2的DEC值爲50? –

+0

它只是,我遵循這個參考翻譯http://www.asciitable.com/ – Eight

+0

這是ascii代碼,而不是DEC值。你不應該讓別人誤解。 –

回答

0

client.read()會在你的情況下返回整數轉換54和50。

嘗試使用這樣的:

c += (char) client.read(); 

我只回答了這個基於我在java中的知識。
糾正我,如果我錯了。

+0

好吧,這是62返回一個字符串,所以現在我只需要將該字符串轉換爲一個int。 – Eight

+0

我用c.toInt(),現在它是一個int。你不知道我現在有多愛你。 – Eight