[英]converting integer into 4 bit binary
I have the following code to convert an integer into a binary representation for each integer from 0 to 15, thus I need 4-bit representation of an integer. 我有以下代码将整数转换为从0到15的每个整数的二进制表示形式,因此我需要4位的整数表示形式。
the code it works fine but the problem is the length of the binary is not correct, so when given 1 as int it returns 1 as output instead of 0001. for 2 it should return 0010 but instead, it returns 10. 代码可以正常工作,但是问题是二进制文件的长度不正确,因此当给定1作为int时,它返回1作为输出而不是0001。对于2,它应该返回0010但返回10。
How can I change this code so it returns the correct representation? 如何更改此代码,使其返回正确的表示形式? Using printf
%04d
is oki when printing the results, but just for printing because the actual value is still different. 在打印结果时可以使用printf
%04d
,但仅用于打印,因为实际值仍然不同。
I was trying to make another method that gets an integer convert it into a string and then depending on its length add 0 before it until the length is 4. 我试图制作另一个获取整数的方法,将其转换为字符串,然后根据其长度在其长度为4之前添加0。
#include <stdio.h>
#include <stdlib.h>
int convert(int dec)
{
if (dec == 0)
{
//printf("Base c\n");
return 0;
}
else
{
//printf("Rec call\n");
return (dec % 2) + 10 * convert(dec / 2);
}
}
int main(int argc, char *argv[])
{
// argc is number of arguments given including a.out in command line
// argv is a list of string containing command line arguments
int v = atoi(argv[2]);
printf("the number is:%d \n", v);
int bin = 0;
if(v >= 0 && v <= 15){
printf("Correct input \n");
bin = convert(v);
printf("Binary is: %04d \n", bin);
printf("Binary is: %d \n", bin);
}
else{
printf("Inorrect input, number cant be accepted");
}
What I need this method to do: 1. given an integer 2. return 4-bit representation of this integer, not sure if it should be an int or string. 我需要此方法执行的操作:1.给定整数2.返回此整数的4位表示形式,不确定是否应为int或字符串。 3. for example convert(2) should return 0010, 6 returns 110, and I want this to be 0110 and so on.
3.例如,convert(2)应该返回0010,6应该返回110,我希望它是0110,依此类推。 the method convert was supposed to do that for me.
该方法转换应该为我做到这一点。 I hope I am clear about what I what to happen.
我希望我能清楚自己会发生什么。
this should return for: 这应该返回:
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
and so on
15 1111
Your requirements are very unclear but from the comments I think I have gathered what you require. 您的要求还不清楚,但是从评论中我认为我已经收集了您的要求。 According to my suggestion, you need to change the function to return a string instead of an
int
. 根据我的建议,您需要更改函数以返回字符串而不是
int
。
You need to pass an argument in which the string should be returned. 您需要传递一个应在其中返回字符串的参数。 So the function will be -
因此功能将是-
char * convert(int dec, char *output) {
output[4] = '\0';
output[3] = (dec & 1) + '0';
output[2] = ((dec >> 1) & 1) + '0';
output[1] = ((dec >> 2) & 1) + '0';
output[0] = ((dec >> 3) & 1) + '0';
return output;
}
This function can be used as 该功能可以用作
char binary[5];
convert(15, binary);
printf("%s", binary);
Initialize a character array of size 5, ex: char arr[5]
and mark arr[4] ='\\0'
, rest of the slots as 0 and then store the LSB into the 4th array slot & MSB near the 1st array slot. 初始化一个大小为5的字符数组,例如:
char arr[5]
并标记arr[4] ='\\0'
,将其余插槽设置为0 ,然后将LSB存储到第4个数组插槽中,并将MSB存储在第一个数组插槽附近。 Print it using %s
format specifier. 使用
%s
格式说明符进行打印。
Initially the array will look like |0|0|0|0|\\0|
最初,数组看起来像
|0|0|0|0|\\0|
. 。 Let's say that your input is 5 and the output you receive is (in form of int ) 101 , then after inserting the output into the array will look like
|0|1|0|1|\\0|
假设您的输入为5 ,而您收到的输出为(以int形式) 101 ,那么在将输出插入到数组中后,看起来将是
|0|1|0|1|\\0|
. 。
My attempt 我的尝试
#include <stdio.h>
#include <stdlib.h>
int convert(int dec)
{
if (dec == 0)
{
//printf("Base c\n");
return 0;
}
else
{
//printf("Rec call\n");
return (dec % 2) + 10 * convert(dec / 2);
}
}
int main()
{
int binary = 10;
binary = convert(binary);
printf("%d", binary);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.