2016-08-23 166 views
-4

嗨,我很新的編碼,我真的需要幫助。 基本上我有一個十進制值,我把它轉換成二進制值。 使用這種方法需要將int轉換爲字符串使用C

long decimalToBinary(long n) 
{ 
    int remainder; 
    long binary = 0, i = 1; 

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

而且我想給二進制的每一個字符到一個數組裏面它自己的空間。但是,我似乎無法保存我的字符串數組中的返回值的數字。我認爲這與將長轉換爲字符串有關,但我可能是錯的!這是我到目前爲止。 我不想使用sprintf();我不希望打印出值,我只是想將值存儲在裏面,以便if條件可以讀取它。任何幫助,將不勝感激!

int decimalG = 24; 
long binaryG = decimalToBinary(decimalG); 
char myStringG[8] = {binaryG}; 
for(int i = 0; i<8; i++) 
{ 
    if (myStringG[i] == '1') 
    { 
    T1(); 
    } 
    else 
    { 
    T0(); 
} 
} 

在這種情況下,由於十進制是24,二進制將是11000,因此它應該執行函數T1(); 2次和T0()6次。但它不這樣做,我似乎無法找到答案將保存的值存儲在數組中。 * Itoa();功能也不是一個選項。提前致謝! :)

+3

變量不具有「十進制值」 - 除非使用數字系統,小數存儲器單元。不太可能,所有現代計算機都是**二進制**。而C不支持_methods_,只有_functions_。 – Olaf

+0

退後一步,看看'decimalToBinary'實際做了什麼。這是我們的廢話。只要刪除整個身體和'返回n;'。沒有冒犯,但你真的搞砸了。它甚至不清楚你實際想要完成什麼。你知道'itoa'(小寫!),你似乎有不同的想法。 – Olaf

+0

'char myStringG [8 + 1]; snprintf(myStringG,sizeof(myStringG),「%08ld」,binaryG);' – BLUEPIXY

回答

0

使用itoa(integer-to-ascii)函數。

http://www.cplusplus.com/reference/cstdlib/itoa/


編輯:校正:

不要白癡,使用itoa(整數到ASCII)函數。

http://www.cplusplus.com/reference/cstdlib/itoa/


編輯:

也許我還不夠清楚。我看到那行說:

* Ps the Itoa();功能也不是一個選項。

這是完全不合理的。你想重新發明輪子,但你想要別人去做嗎?你可能對itoa有什麼反應?這是標準的一部分。無論您使用的是哪種平臺或C版本,它都會一直存在。

+2

問題說*「Itoa()'函數也不是一個選項」*。 – Pang

0

爲了達到你的目標,你不必爲十進制值轉換爲二進制:

unsigned decimalG = 24; // Assumed positive, for negative values 
         // have implementation-defined representation 
for (; decimalG; decimalG >>= 1) { 
    if(decimalG & 1) { 
     // Do something 
    } else { 
     // Do something else 
    } 
} 

或者你可以使用一個工會,但我不知道是否這種方法是由明確定義標準。


如果你堅持寫decimalToBinary,請注意,你必須使用一個數組:

#include <stdio.h> 
#include <stdlib.h> 
#include <limits.h> 

char *decimalToBinary(unsigned n); 

int 
main(void) { 
    int decimalG = 15; 
    char *binary = decimalToBinary(decimalG); 
    puts(binary); 
    free(binary); 
} 

char * 
decimalToBinary(unsigned n) { 
    // Don't forget to free() it after use!!! 
    char *binary = malloc(sizeof(unsigned) * CHAR_BIT + 1); 
    if(!binary) return 0; 

    size_t i; 
    for (i = 0; i < sizeof(unsigned) * CHAR_BIT; i++) { 
     binary[i] = '0' + ((n >> i) & 1); // in reverse order 
    } 
    binary[i] = 0; 

    return binary; 
} 
1

視後使用malloc()可能不是最好的方法標記arm,雖然簡單。如果你堅持使用陣列上:

#include <stdio.h> 
#include <stdlib.h> 

int decimalToBinary(long n, char out[], int len) 
{ 
    long remainder; 
    // C arrays are zero based 
    len--; 
    // TODO: check if the input is reasonable 
    while (n != 0) { 
    // pick a bit 
    remainder = n % 2; 
    // shift n one bit to the right 
    // It is the same as n = n/2 but 
    // is more telling of what you are doing: 
    // shifting the whole thing to the right 
    // and drop the least significant bit 
    n >>= 1; 
    // Check boundaries! Always! 
    if (len < 0) { 
     // return zero for "Fail" 
     return 0; 
    } 
    // doing the following four things at once: 
    // cast remainder to char 
    // add the numerical value of the digit "0" 
    // put it into the array at place len 
    // decrement len 
    out[len--] = (char) remainder + '0'; 
    } 
    // return non-zero value for "All OK" 
    return 1; 
} 

// I don't know what you do here, but it 
// doesn't matter at all for this example 
void T0() 
{ 
    fputc('0', stdout); 
} 

void T1() 
{ 
    fputc('1', stdout); 
} 

int main() 
{ 
    // your input 
    int decimalG = 24; 
    // an array able to hold 8 (eight) elements of type char 
    char myStringG[8]; 

    // call decimalToBinary with the number, the array and 
    // the length of that array 
    if (!decimalToBinary(decimalG, myStringG, 8)) { 
    fprintf(stderr, "decimalToBinary failed\n"); 
    exit(EXIT_FAILURE); 
    } 
    // Print the whole array 
    // How to get rid of the leading zeros is left to you 
    for (int i = 0; i < 8; i++) { 
    if (myStringG[i] == '1') { 
     T1(); 
    } else { 
     T0(); 
    } 
    } 
    // just for the optics 
    fputc('\n', stdout); 
    exit(EXIT_SUCCESS); 
} 

計算所需要的長度是棘手的,但如果你知道的long您的Micro採用(8,16,32,甚至64位的這些天)的大小,你可以採取作爲陣列的最大尺寸。留下前導零,但這不應該是一個問題,或者它?

0

我想將二進制的每個字符放入它自己的 數組中。但是,我似乎無法保存我的字符串數組中的返回值的數字 。

有很多方法可以解決這個問題,如果我明白你在問什麼。首先,根據組成數字的任何給定位的位值,不需要將數字的二進制表示形式的結果實際存儲在數組中,以便調用T1()T0()

舉例24(二進制11000)。如果我正確地讀您的文章,你陳述:

在這種情況下,由於小數是24,二進制 將11000因此應執行的 功能T1()2時間和T0()6倍。

(我不知道你在哪裏得到6時候,它看起來像你打算T0()將被稱爲3次)

如果你有T0T1定義,例如,簡單地讓你知道他們什麼時候被叫,例如:

void T1 (void) { puts ("T1 called"); } 
void T0 (void) { puts ("T0 called"); } 

您可以編寫一個函數(比如命名callt)只需撥打T1每個1-bitT0每個0-bit在數字如下:

void callt (const unsigned long v) 
{ 
    if (!v) { putchar ('0'); return; }; 

    size_t sz = sizeof v * CHAR_BIT; 
    unsigned long rem = 0; 

    while (sz--) 
     if ((rem = v >> sz)) { 
      if (rem & 1) 
       T1(); 
      else 
       T0(); 
     } 
} 

到目前爲止,例如,如果你通過24到功能callt (24),輸出將是:

$ ./bin/dec2bincallt 
T1 called 
T1 called 
T0 called 
T0 called 
T0 called 

在另一方面,如果你確實想給二進制的每個字符到它自己的空間陣列內(在回答結束提供完整的例子),那麼你就只需要通過一個陣列捕獲位值(無論是'0''1',或者只是01的ASCII字符表示),而不是調用T0T1(你也添加幾行來處理v=0,也是NUL終止,如果字符您將使用數組作爲字符串)例如:

/** copy 'sz' bits of the binary representation of 'v' to 's'. 
* returns pointer to 's', on success, empty string otherwise. 
* 's' must be adequately sized to hold 'sz + 1' bytes. 
*/ 
char *bincpy (char *s, unsigned long v, unsigned sz) 
{ 
    if (!s || !sz) { 
     *s = 0; 
     return s; 
    } 
    if (!v) { 
     *s = '0'; 
     *(s + 1) = 0; 
     return s; 
    } 
    unsigned i; 

    for (i = 0; i < sz; i++) 
     s[i] = (v >> (sz - 1 - i)) & 1 ? '1' : '0'; 
    s[sz] = 0; 

    return s; 
} 

讓我知道你是否還有其他問題。以下是兩個示例程序。兩者都將作爲第一個參數的數字轉換(或處理)爲二進制(默認:如果沒有給出參數,則爲24)。第一隻是調用T1爲每個0-bit1-bitT0

#include <stdio.h> 
#include <stdlib.h> 
#include <limits.h>   /* for CHAR_BIT */ 

void callt (const unsigned long v); 
void T1 (void) { puts ("T1 called"); } 
void T0 (void) { puts ("T0 called"); } 

int main (int argc, char **argv) { 

    unsigned long v = argc > 1 ? strtoul (argv[1], NULL, 10) : 24; 

    callt (v); 

    return 0; 
} 

void callt (const unsigned long v) 
{ 
    if (!v) { putchar ('0'); return; }; 

    size_t sz = sizeof v * CHAR_BIT; 
    unsigned long rem = 0; 

    while (sz--) 
     if ((rem = v >> sz)) { 
      if (rem & 1) T1(); else T0(); 
     } 
} 

實施例使用/輸出

$ ./bin/dec2bincallt 
T1 called 
T1 called 
T0 called 
T0 called 
T0 called 

$ ./bin/dec2bincallt 11 
T1 called 
T0 called 
T1 called 
T1 called 

第二存儲該值的二進制表示的每個比特作爲NUL - 終止字符串並打印結果:

#include <stdio.h> 
#include <stdlib.h> 

#define BITS_PER_LONG 64 /* define as needed */ 

char *bincpy (char *s, unsigned long v, unsigned sz); 

int main (int argc, char **argv) { 

    unsigned long v = argc > 1 ? strtoul (argv[1], NULL, 10) : 24; 
    char array[BITS_PER_LONG + 1] = ""; 

    printf (" values in array: %s\n", bincpy (array, v, 16)); 

    return 0; 
} 

/** copy 'sz' bits of the binary representation of 'v' to 's'. 
* returns pointer to 's', on success, empty string otherwise. 
* 's' must be adequately sized to hold 'sz + 1' bytes. 
*/ 
char *bincpy (char *s, unsigned long v, unsigned sz) 
{ 
    if (!s || !sz) { 
     *s = 0; 
     return s; 
    } 
    if (!v) { 
     *s = '0'; 
     *(s + 1) = 0; 
     return s; 
    } 
    unsigned i; 

    for (i = 0; i < sz; i++) 
     s[i] = (v >> (sz - 1 - i)) & 1 ? '1' : '0'; 
    s[sz] = 0; 

    return s; 
} 

實施例使用/輸出

(填充到16個比特)

$ ./bin/dec2binarray 
values in array: 0000000000011000 

$ ./bin/dec2binarray 11 
values in array: 0000000000001011