gpt4 book ai didi

在 C 中将二进制格式字符串转换为 int

转载 作者:太空狗 更新时间:2023-10-29 16:57:41 29 4
gpt4 key购买 nike

如何在 C 语言中将二进制字符串(如“010011101”)转换为 int,以及如何将 int(如 5)转换为字符串“101”?

最佳答案

标准库中的 strtol 函数采用“基本”参数,在本例中为 2。

int fromBinary(const char *s) {
return (int) strtol(s, NULL, 2);
}

(我大约 8 年来编写的第一个 C 代码 :-)

关于在 C 中将二进制格式字符串转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2343099/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com