用C转换ASCII到二进制 [英] Converting Ascii to binary in C

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

问题描述

因此​​,这里是我的code

So here is my code

void ascToBinary(int character, int *ones)
{
    if(character == 1)
    {
       printf("1");
       *ones+=1;
       return;
    }
    else
    {
        if((character%2) == 0)
        {
             printf("0");
             character = character/2;
        }
        else
        {
             printf("1");
             character = character/2;
             *ones+=1;

        }
        binaryPrinter(character, ones);
    }
}

任何人都可以尝试帮助我在哪里什么是错在这里。它编译罚款和做一些ASCII字母正确的。如果您尝试一个e,虽然它会打印出1010011,而不是正确的二进制文件。

Can anyone try to help me out on where something is wrong here. It compiles fine and does some ascii letters correct. If you try an 'e' though it will print out '1010011' instead of the correct binary.

所有帮助AP preciated谢谢你们。

All help appreciated Thanks Guys.

推荐答案

您打印以错误的顺序的结果。正确的输出是'1100101'。你可以将其翻转像这样:

You print the results in the wrong order. The correct output is '1100101'. You can flip it like this:

    void ascToBinary(int character, int *ones)
    {
        if(character == 1)
        {
           printf("1");
           *ones+=1;
           return;
        }
        else
        {
            char out;
            if((character%2) == 0)
            {
                 out = '0';
                 character = character/2;
            }
            else
            {
                 out = '1';
                 character = character/2;
                 *ones+=1;

            }
            ascToBinary(character, ones);
            putchar (out);
        }
    }

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

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