[英]convert each digit of a decimal number to correcsponding binary
我需要將字符串“ 12345678”轉換為值00010010001101101000101011001111000(二進制值,僅左側不帶零)。 所以我已經用c編寫了這段代碼,問題是當我運行它時,它什么也沒做,只是等待,直到出現錯誤為止,直到我手動停止它為止。 有任何想法嗎?
#include <stdio.h>
#include <string.h>
void reduce(char string[]) {
int i=0, j=0, k=0, cnt=0, tmp=4, num;
char arr[4], result[4*strlen(string)];
for (i=0; i<strlen(string); i++) {
num = atoi(string[i]);
while (num != 0) {
arr[j++] = num%2;
num = num/2;
tmp--;
}
while (tmp != 0) {
arr[j++] = 0;
tmp--;
}
j--;
for (k=i*4; k<(i*4+4); k++) {
result[k++] = arr[j--];
}
j = 0;
tmp = 4;
}
printf("The result is: \n");
for (i=0; i<4*strlen(result); i++) {
printf("%d",result[i]);
}
printf("\n");
}
int main() {
char c[8] = "12345678";
reduce(c);
return 0;
}
您的代碼中有許多小錯誤,這使得很難查明單個錯誤。 主要問題似乎是你混淆二進制數字( 0
, 1
)用ASCII數字( "0"
, "1"
)和被誤用字符串函數。
char c[8] = ..
是錯誤的。 atoi(string[i])
無法工作; 它需要一個字符串 ,而不是char 。 使用`num = string [i]-'0'; arr[..]
獲得值'num%2
,即一個數值。 最好使用'0'+num%2
因為它是一個字符串。 k
在result[k++]
已經增加一個循環內k
result[k] = 0;
在打印之前結束,因此strlen
可以正常工作 4*strlen(result)
太多了strlen
是它的意思。 printf("%s\\n", result);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reduce(char string[]) {
int i=0, j=0, k=0, cnt=0, tmp=4, num;
char arr[5], result[4*strlen(string)+1];
for (i=0; i<strlen(string); i++) {
num = string[i]-'0';
while (num != 0) {
arr[j++] = '0'+num%2;
num = num/2;
tmp--;
}
while (tmp != 0) {
arr[j++] = '0';
tmp--;
}
arr[j] = 0;
j--;
for (k=i*4; k<(i*4+4); k++) {
result[k] = arr[j--];
}
j = 0;
tmp = 4;
}
result[k] = 0;
printf("The result is: \n");
for (i=0; i<strlen(result); i++) {
printf("%c",result[i]);
}
printf("\n");
}
int main() {
char c[] = "12345678";
reduce(c);
return 0;
}
.. 導致
The result is:
00010010001101000101011001111000
在您的main()
,執行以下任一操作
char c[ ] = "12345678";
要么
char c[9] = "12345678";
如果要使用c
作為字符串。 否則,它沒有足夠的空間來存儲終止空字符。
在這里,我可以自由地修改代碼以適合您。 檢查以下代碼。 希望這是自我解釋。
#include <stdio.h>
#include <string.h>
void reduce(char string[]) {
int i=0, j=0, k=0, cnt=0, count = 0; //count added, tmp removed
char arr[4], result[ (4*strlen(string) )+ 1], c; //1 more byte space to hold null
for (i=0; i<strlen(string); i++) {
c = string[i];
count = 4;
while (count != 0) { //constant iteration 4 times baed on 9 = 1001
arr[j++] = '0' + (c%2); //to store ASCII 0 or 1 [48/ 49]
c = c/2;
count--;
}
/* //not required
while (tmp >= 0) {
arr[j++] = 0;
tmp--;
}
*/
j--;
for (k=(i*4); k<((i*4) +4); k++) {
result[k] = arr[j--];
}
j = 0;
memset (arr, 0, sizeof(arr));
}
result[k] = 0;
printf("The result is: %s\n", result); //why to loop when we've added the terminating null? print directly.
/*
for (i=0; i< strlen(result); i++) {
printf("%c",result[i]);
}
printf("\n");
*/
}
int main() {
char c[ ] = "12345678";
reduce(c);
return 0;
}
輸出:
[sourav@broadsword temp]$ ./a.out
The result is: 00010010001101000101011001111000
從您的示例看來,您嘗試的轉換是二進制編碼的十進制而不是二進制 。 在這種情況下,您的解決方案有些復雜。 您只需要將每個數字轉換為其整數值,然后將位模式轉換為ASCII 1
和0
。
#include <stdio.h>
void reduce( const char* c )
{
for( int d = 0; c[d] != 0; d++ )
{
int ci = c[d] - '0' ;
for( unsigned mask = 0x8; mask != 0; mask >>= 1 )
{
putchar( (ci & mask) == 0 ? '0' : '1' ) ;
}
}
}
另一方面,如果您確實打算轉換為二進制(而不是BCD),那么如果將整個字符串轉換為整數 ,則可以將位模式直接轉換為ASCII 1
和0
,如下所示:
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
void reduce( const char* c )
{
unsigned ci = (unsigned)atoi( c ) ;
static const int BITS = sizeof(ci) * CHAR_BIT ;
for( unsigned mask = 0x01 << (BITS - 1); mask != 0; mask >>= 1 )
{
putchar( (ci & mask) == 0 ? '0' : '1' ) ;
}
}
int num = atoi(c)
將字符串轉換為整數。 然后做
int binary[50]; int q = num,i=0; while(q != 0) { binary[i++] = q%2; q = q/2; }
以相反的順序打印二進制數組將具有等效的二進制文件。 完整程序:
#include<stdio.h>
int main(){
char c[100];
int num,q;
int binary[100],i=0,j;
scanf("%d",c);
num = atoi(c);
q = num;
while(q!=0){
binary[i++]= q % 2;
q = q / 2;
}
for(j = i -1 ;j>= 0;j--)
printf("%d",binary[j]);
return 0;
}
您可以使用以下減少功能。
void reduce(char string[])
{
unsigned int in = atoi(string) ;
int i = 0, result[32],k,j;
while (in > 0) {
j = in % 10;
k = 0;
while (j > 0) {
result[i++] = j % 2;
j = j >> 1;
k++;
}
while (k < 4) {
result[i++] = 0;
k++;
}
in = in/10;
}
printf("Result\n");
for(--i;i >= 0; i--) {
printf("%d", result[i]);
}
printf("\n");
}
對於12345678
,輸出將為000100100011010001000101011001111000 ,其中每個字符均以其二進制格式打印。
它可能需要進行一些調整,但它確實可以正常工作。
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
int i;
int n;
char *str = "12345678";
const int bit = 1 << (sizeof(n)*8 - 1);
n = atoi(str);
for(i=0; i < sizeof(n)*8 ; i++, n <<= 1)
n&bit ? printf("1") : printf("0");
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.