转换0和1的串入二进制值 [英] Converting string of 1s and 0s into binary value

查看:178
本文介绍了转换0和1的串入二进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想1和0的传入刺痛转换从标准到它们各自的二进制值(一个字符串,如11110111将被转换为0xF7)。这似乎是pretty微不足道,但我不希望推倒重来,所以我想,如果有一个在C / C ++标准库什么,已经可以执行这样的操作?


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释主要(无效){
    字符* PTR;
    长解析=与strtol(11110111,&安培; PTR,2);
    的printf(%LX \\ n,解析);
    返回EXIT_SUCCESS;
}

有关更大的数字,有一个长长版本,与strtoll

I'm trying to convert an incoming sting of 1s and 0s from stdin into their respective binary values (where a string such as "11110111" would be converted to 0xF7). This seems pretty trivial but I don't want to reinvent the wheel so I'm wondering if there's anything in the C/C++ standard libs that can already perform such an operation?

解决方案

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

int main(void) {
    char * ptr;
    long parsed = strtol("11110111", & ptr, 2);
    printf("%lX\n", parsed);
    return EXIT_SUCCESS;
}

For larger numbers, there as a long long version, strtoll.

这篇关于转换0和1的串入二进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆