C语言中如何将二进制转换成相应的数

C语言中如何将二进制转换成相应的数

问题描述:

请问这个要怎么解决?应该是 输入16 个字符的字符串的情况下,返回相应的有符号整数。在“PUT YOUR CODE HERE”写内容,蟹蟹大lao。题目如下

img

img

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#define N_BITS 16

int16_t sixteen_in(char *bits);

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

    for (int arg = 1; arg < argc; arg++) {
        printf("%d\n", sixteen_in(argv[arg]));
    }

    return 0;
}

//
// given a string of binary digits ('1' and '0')
// return the corresponding signed 16 bit integer
//
int16_t sixteen_in(char *bits) {

    // PUT YOUR CODE HERE

    return 0;
}


#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#define N_BITS 16
int16_t sixteen_in(char *bits);
int main(int argc, char *argv[])
{
    for (int arg = 1; arg < argc; arg++)
    {
    //  printf("1\n");  //测试
        printf("%d\n", sixteen_in(argv[arg]));
    }
    return 0;
}

/*函数定义*/
int16_t sixteen_in(char *bits)
{
    assert(bits!=NULL);  //不为空指针

    int16_t count = 0;
    while(*bits!='\0')
    {
        count++;
        bits++;

    }
//  printf("count=%d\n",count);  //测试
    if(count==16)
    {
        return count;
    }
    else
    {
        return 0;
    }
}

img