2010-08-30 108 views

回答

3

第一種是C型。無需轉換,只是爲它們分配:

byte b = ...; 

int x = b; 
float f = b; 

轉換爲NSString的可利用stringWithFormat:,一個NSNumberFormatter和更多的方法來完成。這是最簡單的:

NSString *myString = [NSString stringWithFormat: @"%d", b]; 

如果你想在十六進制打印,使用@"%x"(小寫字母)或@"%X"(爲大寫字母)代替。

+0

其轉換成整數或浮點數如果我有四個字節數組,我想將其轉換成int類型。 – Ideveloper 2010-08-30 13:39:13

+1

bitshift並添加它們。像這樣:'int x =(arr [0] << 12)+(arr [1] << 8)+(arr [2] << 4)+ arr [3]' – 2010-08-30 13:44:37

+0

這有幫助。非常感謝 – Ideveloper 2010-08-30 13:52:14

0

你可以得到一個NSDataNSStringinitWithData:encoding:然後通過調用intValuefloatValue