字符转化为二进制用C [英] Conversion of Char to Binary in C

查看:109
本文介绍了字符转化为二进制用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个字符转换为其二进制重新presentation(所以字符 - >十六进制ASCII码 - >二进制)。

我知道该怎么做,我需要转移和。然而,我的code不工作的某些原因。

下面是我。 *温度指向C字符串。

索引

 字符℃;
诠释J;
为(J = I-1,J> = ptrPos; j--){
    字符X = *温度;
    C =(X GT; I标记)及1;
    的printf(%d个\\ N,C);
    temp--;
}


解决方案

我们显示,打印一个字符为二进制两种功能。

 无效printbinchar(CHAR字符)
{
    焦炭产量[9];
    itoa(字符,输出,2);
    的printf(%S \\ n,输出);
}

printbinchar(10)将写入到控制台

  1010

itoa是一个整数值转换为指定的基本字符串的库函数。
例如... itoa(1341,输出10)将在输出字符串1341写的。
当然itoa(9,输出,2)将在输出字符串1001写的。

下一个函数将打印到标准输出一个字符的完整二进制重新presentation,即,将打印所有8位,也如果较高位都为零。

 无效printbincharpad(焦三)
{
    的for(int i = 7; I> = 0; --i)
    {
        的putchar((C&安培;(1<< I))'1':'0');
    }
    的putchar('\\ n');
}

printbincharpad(10)将写入到控制台

  00001010

现在我present打印出整个字符串(没有最后的空字符)的功能。

 无效printstringasbinary(字符* S)
{
    //小9个字符缓冲区我们用来执行转换
    焦炭产量[9];    //直到由s指向的第一个字符不是空字符
    //指示字符串的结尾...
    而(* S)
    {
        //将字符串的第一个字符为二进制使用itoa。
        //在C字符是只有8位整数,至少在noawdays电脑。
        itoa(* S,输出,2);        //打印出我们的字符串,让我们写一个新行。
        看跌(输出);        //我们一个字符推进我们的字符串,
        //如果我们的原始字符串是ABC现在我们是在BC指点。
        ++ S;
    }
}

然而

考虑到itoa不添加填充零,所以printstringasbinary(AB1)将打印出类似这样:

  1000001
1000010
110001

I am trying to convert a character to its binary representation (so character --> ascii hex --> binary).

I know to do that I need to shift and AND. However, my code is not working for some reason.

Here is what I have. *temp points to an index in a C string.

char c;
int j;
for (j = i-1; j >= ptrPos; j--) {
    char x = *temp;
    c = (x >> i) & 1;
    printf("%d\n", c);
    temp--;
}

解决方案

We show up two functions that prints a SINGLE character to binary.

void printbinchar(char character)
{
    char output[9];
    itoa(character, output, 2);
    printf("%s\n", output);
}

printbinchar(10) will write into the console

    1010

itoa is a library function that converts a single integer value to a string with the specified base. For example... itoa(1341, output, 10) will write in output string "1341". And of course itoa(9, output, 2) will write in the output string "1001".

The next function will print into the standard output the full binary representation of a character, that is, it will print all 8 bits, also if the higher bits are zero.

void printbincharpad(char c)
{
    for (int i = 7; i >= 0; --i)
    {
        putchar( (c & (1 << i)) ? '1' : '0' );
    }
    putchar('\n');
}

printbincharpad(10) will write into the console

    00001010

Now i present a function that prints out an entire string (without last null character).

void printstringasbinary(char* s)
{
    // A small 9 characters buffer we use to perform the conversion
    char output[9];

    // Until the first character pointed by s is not a null character
    // that indicates end of string...
    while (*s)
    {
        // Convert the first character of the string to binary using itoa.
        // Characters in c are just 8 bit integers, at least, in noawdays computers.
        itoa(*s, output, 2);

        // print out our string and let's write a new line.
        puts(output);

        // we advance our string by one character,
        // If our original string was "ABC" now we are pointing at "BC".
        ++s;
    }
}

Consider however that itoa don't adds padding zeroes, so printstringasbinary("AB1") will print something like:

1000001
1000010
110001

这篇关于字符转化为二进制用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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