简体   繁体   English

将int,double转换为C或Objective C中的char数组

[英]Converting int, double to char array in C or Objective C

I want to convert int to char array and double to char array. 我想将int转换为char数组,并将double转换为char数组。 Any way of doing this in C or Objective C. 在C或目标C中执行此操作的任何方式。

If you want to treat the number as an array of char , you can take the address and cast the pointer: 如果要将数字视为char数组,则可以获取地址并强制转换指针:

int i;
double d;
char * ic = (char *) &i;
char * dc = (char *) &d;

then ic and dc are pointers to char . 那么icdcchar指针。 They aren't zero-terminated, so you can't use them as strings, but they can be used as arrays. 它们不是零结尾的,因此您不能将它们用作字符串,但是可以将它们用作数组。

For another interpretation of what "converting" means, use 对于“转换”的含义的另一种解释,请使用

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM