[英]convert each digit of a decimal number to correcsponding binary
I need to convert the string "12345678" to the value 00010010001101000101011001111000 (the value in binary only without the zeroes on the left). 我需要将字符串“ 12345678”转换为值00010010001101101000101011001111000(二进制值,仅左侧不带零)。 So I have written this code in c, the problem is that when I run it does nothing, just waits like there is an error until I stop it manually.
所以我已经用c编写了这段代码,问题是当我运行它时,它什么也没做,只是等待,直到出现错误为止,直到我手动停止它为止。 Any ideas?
有任何想法吗?
#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;
}
Lots of small errors in your code, which makes it hard to pin-point a single error. 您的代码中有许多小错误,这使得很难查明单个错误。 Main problem seems to be you are confusing binary numbers (
0
, 1
) with ASCII digits ( "0"
, "1"
) and are mis-using string functions. 主要问题似乎是你混淆二进制数字(
0
, 1
)用ASCII数字( "0"
, "1"
)和被误用字符串函数。
char c[8] = ..
is wrong. char c[8] = ..
是错误的。 atoi(string[i])
cannot work; atoi(string[i])
无法工作; it expects a string , not a char . arr[..]
gets the value 'num%2
, that is, a numerical value. arr[..]
获得值'num%2
,即一个数值。 Better to use '0'+num%2
so it's a character string. '0'+num%2
因为它是一个字符串。 k
in result[k++]
inside a loop that already increments k
k
在result[k++]
已经增加一个循环内k
result[k] = 0;
result[k] = 0;
at the end before printing, so strlen
works correctly strlen
可以正常工作 4*strlen(result)
is way too much -- the strlen
is what it is. 4*strlen(result)
太多了strlen
是它的意思。 printf("%s\\n", result);
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;
}
.. resulting in .. 导致
The result is:
00010010001101000101011001111000
In your main()
, do either 在您的
main()
,执行以下任一操作
char c[ ] = "12345678";
or 要么
char c[9] = "12345678";
if you want to use c
as a string. 如果要使用
c
作为字符串。 Otherwise, it does not have enough space to store the terminating null character. 否则,它没有足够的空间来存储终止空字符。
Here, I took the liberty to modify the code accordingly to work for you. 在这里,我可以自由地修改代码以适合您。 Check the below code.
检查以下代码。 Hope it's self-explanatoty.
希望这是自我解释。
#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;
}
Output: 输出:
[sourav@broadsword temp]$ ./a.out
The result is: 00010010001101000101011001111000
It seems from your example that the conversion you are attempting is to binary coded decimal rather than binary . 从您的示例看来,您尝试的转换是二进制编码的十进制而不是二进制 。 That being the case your solution is somewhat over-complicated;
在这种情况下,您的解决方案有些复杂。 you simply need to convert each digit to its integer value then translate the bit pattern to ASCII
1
's and 0
's. 您只需要将每个数字转换为其整数值,然后将位模式转换为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' ) ;
}
}
}
On the other hand if you did intend a conversion to binary (rather than BCD), then if the entire string is converted to an integer , you can directly translate the bit pattern to ASCII 1
's and 0
's as follows: 另一方面,如果您确实打算转换为二进制(而不是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 num = atoi(c)
将字符串转换为整数。 Then do 然后做
int binary[50]; int q = num,i=0; while(q != 0) { binary[i++] = q%2; q = q/2; }
Printing your binary array is reverse order will have your binary equivalent. 以相反的顺序打印二进制数组将具有等效的二进制文件。 Full program:
完整程序:
#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;
}
You can use the below reduce function. 您可以使用以下减少功能。
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");
}
For 12345678
the output would be 00010010001101000101011001111000 , where each character is printed in its binary format. 对于
12345678
,输出将为000100100011010001000101011001111000 ,其中每个字符均以其二进制格式打印。
It might need some adjustments, but it does the job as it is. 它可能需要进行一些调整,但它确实可以正常工作。
#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.