将字符串的ASCII字符十进制值转换为二进制值
converting string of ascii character decimal values to binary values
我需要帮助编写一个将完整句子转换为二进制代码(ascii ->十进制->二进制)的程序,反之亦然,但我在做这件事时遇到了麻烦。现在我正在处理ascii->二进制。
ascii字符有十进制值。a = 97
, b = 98
等。我想获得ascii字符的十进制值,并将其转换为普通或二进制十进制,例如二进制中的10(十进制):
10 (decimal) == 1010 (binary)
所以a和b的ascii十进制值是:
97, 98
这在二进制中是(加上空格字符是32,谢谢):
11000011000001100010 == "a b"
11000011100010 == "ab"
我写了这个:
int c_to_b(char c)
{
return (printf("%d", (c ^= 64 ^= 32 ^= 16 ^= 8 ^= 4 ^= 2 ^= 1 ^= 0));
}
int s_to_b(char *s)
{
long bin_buf = 0;
for (int i = 0; s[i] != ' '; i++)
{
bin_buf += s[i] ^= 64 ^= 32 ^= 16 ^= 8 ^= 4 ^= 2 ^= 1 ^= 0;
}
return printf("%d", bin_buf);
}
代码示例
c
int main(void)
{
// this should print out each binary value for each character in this string
// eg: h = 104, e = 101
// print decimal to binary 104 and 101 which would be equivalent to:
// 11010001100101
// s_to_b returns printf so it should print automatically
s_to_b("hello, world!");
return 0;
}
为了详细说明,第二个代码片段中的for
循环遍历字符数组中的每个字符,直到它遇到空终止符。每次计算一个字符,它都会做那个操作。我是否使用了正确的操作?
也许你想要像
void s_to_b(const char*s)
{
if (s != NULL) {
while (*s) {
int c = *s;
printf(" %d", c);
s++;
}
putc('n');
}
}
相关文章:
- visual在c++中将十进制数转换为二进制数
- C++十进制到二进制,如何转换
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- C++从二进制转换为十进制数
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 如何在 O(n) 中将二进制和转换为二进制
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 十进制到二进制转换器 c++
- 在C++中将结构转换和存储为二进制
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 如何在C ++中将二进制字符串128位转换为十进制字符串?
- 如何将二进制文件头示例代码 (C++/C#) 转换为 Python?
- 二进制 '==':未找到采用 'Enemy' 类型左侧操作数的运算符(或者没有可接受的转换)
- 将二进制转换为十进制,我如何要求用户仅输入二进制
- QTextStream的操作数到二进制的转换无效
- 在c++中读取和转换二进制数据
- c++转换二进制到十进制从字符串输入
- 计算器转换二进制到浮点值-我做错了什么