arm - Need to convert int to string using C -


hi pretty new coding , need help. have decimal value , converted binary value. using method

long decimaltobinary(long n)  {     int remainder;      long binary = 0, = 1;      while(n != 0)      {         remainder = n%2;         n = n/2;         binary= binary + (remainder*i);         = i*10;     }     return binary; } 

and want give each character of binary it's own space inside array. however, can't seem save digits return values in string array. think has converting long string wrong! here have far. not want use sprintf(); not wish print value want value stored inside if conditions can read it. appreciated!

int decimalg = 24; long binaryg = decimaltobinary(decimalg); char mystringg[8] = {binaryg}; for( int = 0; i<8; i++) {      if (mystringg[i] == '1' )     {      t1();     }     else      {     t0();  }  } 

in case since decimal 24, binary 11000 therefore should execute the function t1(); 2 times , t0() 6 times. doesn't , can't seem find answer store saved values in array.
*ps itoa(); function not option. in advance! :)

as post tagged arm using malloc() might not best approach, although simplest. if insist on using arrays:

#include <stdio.h> #include <stdlib.h>  int decimaltobinary(long n, char out[], int len) {   long remainder;   // c arrays 0 based   len--;   // todo: check if input reasonable   while (n != 0) {     // pick bit     remainder = n % 2;     // shift n 1 bit right     // same n = n/2     // more telling of doing:     // shifting whole thing right     // , drop least significant bit     n >>= 1;     // check boundaries! always!     if (len < 0) {       // return 0 "fail"       return 0;     }     // doing following 4 things @ once:     // cast remainder char     // add numerical value of digit "0"     // put array @ place len     // decrement len     out[len--] = (char) remainder + '0';   }   // return non-zero value "all ok"   return 1; }  // don't know here, // doesn't matter @ example void t0() {   fputc('0', stdout); }  void t1() {   fputc('1', stdout); }  int main() {   // input   int decimalg = 24;   // array able hold 8 (eight) elements of type char   char mystringg[8];    // call decimaltobinary number, array ,   // length of array   if (!decimaltobinary(decimalg, mystringg, 8)) {     fprintf(stderr, "decimaltobinary failed\n");     exit(exit_failure);   }   // print whole array   // how rid of leading zeros left   (int = 0; < 8; i++) {     if (mystringg[i] == '1') {       t1();     } else {       t0();     }   }   // optics   fputc('\n', stdout);   exit(exit_success); } 

computing length needed tricky, if know size of long micro uses (8, 16, 32, or 64 bit these days) can take maximum size array. leaves leading zeros should not problem, or it?


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

php - What are the best practices for creatiang a "settings" model in Laravel 5? -