【问题标题】:How to convert ascii string to binary?如何将ascii字符串转换为二进制?
【发布时间】:2016-01-18 18:08:50
【问题描述】:

我正在尝试将 ascii 字符串转换为 C 中的二进制字符串。我找到了这个示例 Converting Ascii to binary in C,但我宁愿不使用递归函数。我尝试编写迭代函数而不是递归函数,但二进制字符串缺少前导数字。我正在使用 itoa 来转换字符串,但是 itoa 是一个非标准函数,所以我使用了 What is the proper way of implementing a good "itoa()" function? 的实现,它由 Minh Nguyen 提供。

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int32_t ascii_to_binary(char *input, char **out, uint64_t len)
{
    uint32_t i;
    uint32_t str_len = len * 8;

    if(len == 0)
    {
        printf("Length argument is zero\n");
        return (-1);
    }

    (*out) = malloc(str_len + 1);
    if((*out) == NULL)
    {
        printf("Can't allocate binary string: %s\n", strerror(errno));
        return (-1);
    }

    if(memset((*out), 0, (str_len)) == NULL)
    {
        printf("Can't initialize memory to zero: %s\n", strerror(errno));
        return (-1);
    }

    for(i = 0; i < len; i++)
        itoa((int32_t)input[i], &(*out)[(i * 8)], 2);

    (*out)[str_len] = '\0';

    return (str_len);
}

int main(void)
{
    int32_t rtrn = 0;
    char *buffer = NULL;

    rtrn = ascii_to_binary("a", &buffer, 1);
    if(rtrn < 0)
    {
        printf("Can't convert string\n");
        return (-1);
    }

    printf("str: %s\n", buffer);

    return (0);
}

我得到1100001 for ascii 字符a,但我应该得到01100001,那么如何将 ascii 字符串转换为整个二进制字符串?

【问题讨论】:

  • 只是一个评论:你不需要测试memset的结果。
  • itoa 不会产生前导零。例如,以 10 为底,你永远不会得到 097。
  • @Ian Abbott,很高兴知道。
  • @Jongware 所以itoa 不会放一个前导零,但会放一个前导?
  • 嗯嗯。有很大的不同,不是吗?任何数字都可以有无数个前导零,它不会改变任何事情。但是添加或省略一个(或 4,或在使用十六进制时F)并且它的值发生变化。

标签: c binary ascii


【解决方案1】:

您可以将for 循环更改为如下内容:

for(i = 0; i < len; i++) {
    unsigned char ch = input[i];
    char *o = *out + 8 * i;
    int b;

    for (b = 7; b >= 0; b--)
        *o++ = (ch & (1 << b)) ? '1' : '0';
}

或类似的:

for(i = 0; i < len; i++) {
    unsigned char ch = input[i];
    char *o = &(*out)[8 * i];
    unsigned char b;

    for (b = 0x80; b; b >>= 1)
        *o++ = ch & b ? '1' : '0';
}

【讨论】:

  • 这些都没有在启用地址清理程序的情况下崩溃,这是您的编辑之一崩溃了。这两个例子都很好用,谢谢。
【解决方案2】:

此程序获取整数(包含 32 位)并将其转换为二进制,对其进行处理以使其适用于 ascii 字符串:

#include <stdio.h>

int main()
{
  int n, c, k;

  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);

  printf("%d in binary number system is:\n", n);

  for (c = 31; c >= 0; c--)
  {
    k = n >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("\n");

  return 0;
}

【讨论】:

    【解决方案3】:

    最好只写一个简单的函数来使用位运算符...

    #define ON_BIT = 0x01
    
    char *strToBin(char c) {
        static  char    strOutput[10];
        int     bit;
    
        /*Shifting bits to the right, but don't want the output to be in reverse
         * so indexing bytes with this...
         */
        int     byte;
    
        /* Add a nul at byte 9 to terminate. */
        strOutput[8] = '\0';
    
        for (bit = 0, byte = 7; bit < 8; bit++, byte--) {
            /* Shifting the bits in c to the right, each time and'ing it with
             * 0x01 (00000001).
             */
            if ((c >> bit) & BIT_ON)
                /* We know this is a 1. */
                strOutput[byte] = '1';
            else
                strOutput[byte] = '0';
        }
    
        return strOutput;
    }
    

    类似的东西应该可以,有很多方法可以做到。希望这会有所帮助。

    【讨论】:

    • 你为什么要从一个只返回一个字符的函数中返回char strOutput[10];?
    • 嘿,好点 - 这应该返回一个字符 * - 我的立场是正确的,干杯。
    • 编辑后的函数现在看起来很好,虽然它不是可重入的,而且每次调用都会覆盖旧结果,除非调用者将它们复制到其他地方。
    猜你喜欢
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2012-03-30
    • 2010-10-18
    • 2020-06-14
    相关资源
    最近更新 更多