【问题标题】:Convert text string to binary ASCII in an Arduino sketch在 Arduino 草图中将文本字符串转换为二进制 ASCII
【发布时间】:2013-03-09 01:31:02
【问题描述】:

我正在为 Arduino 编写一个草图,旨在将文本字符串转换为二进制 7 位或 8 位 ASCII。例如,“Hello World”会变成这个 8 位 ASCII 二进制流:

0100100001100101011011000110110001101111001000000111011101101111011100100110110001100100

如您所见,这是标准的 7 位 ASCII,用零填充以使其成为 8 位 ASCII。我不介意我使用哪个位长,只要我开始后它是一致的。我花了几个小时试图找出一种方法来实现这一目标,但无济于事。我最接近的是这样的:

char text[] = "Hello world";

当像这样打印到监视器时:

Serial.println(text[0], BIN);

给我 1001000。但是,这根本不是填充的(所以“0”只是 0,而不是 0000000),显然这并没有为我提供任何可以使用的东西,只是一些可以看的东西!有人对我有什么建议吗?

【问题讨论】:

    标签: binary arduino ascii


    【解决方案1】:

    您可以以此为起点:

    char inputChar = 'H';
    
    // This will 'output' the binary representation of 'inputChar' as 8 characters of '1's and '0's, MSB first.
    for ( uint8_t bitMask = 128; bitMask != 0; bitMask = bitMask >> 1 ) {
      if ( inputChar & bitMask ) {
        output('1');
      } else {
        output('0');
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多