[英]Converting int, double to char array in C or Objective C
我想將int轉換為char數組,並將double轉換為char數組。 在C或目標C中執行此操作的任何方式。
如果要將數字視為char
數組,則可以獲取地址並強制轉換指針:
int i;
double d;
char * ic = (char *) &i;
char * dc = (char *) &d;
那么ic
和dc
是char
指針。 它們不是零結尾的,因此您不能將它們用作字符串,但是可以將它們用作數組。
對於“轉換”的含義的另一種解釋,請使用
union double_or_bytes { double d ; char bytes[8] ; } converter;
converter.d = <the double you have> ;
<do what you wanted to do with> converter.bytes ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.