【问题标题】:How can I change string into ASCII and then binary in C?如何将字符串更改为 ASCII,然后在 C 中更改为二进制?
【发布时间】:2022-10-05 14:01:24
【问题描述】:

所以我正在解决一个问题,要求我将用户输入字符串更改为 ASCII,然后以递归方式二进制。我设法为单个字符执行此操作,但是当我尝试使用 for 循环时,它会给我带来一个奇怪的结果。谁能帮我解决这个问题?

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
    char toBS;
    printf(\"Enter the string you want to convert: \\n\"); 
    scanf(\"%c\", &toBS);
    int i;
    for (i = 0; i<toBS+1; i++){
        print(toBS);
    }
    printf(\"\\n\"); 
    return;
}

void print(char c)
{
    int n = CHAR_BIT;
    binaryPrinter((unsigned char)c, &n);
    putchar(\'\\n\');
}

void binaryPrinter(int value, int *numberOfOnes)
{
    if((*numberOfOnes)--){
        binaryPrinter(value >> 1, numberOfOnes);
        printf(\"%d\", value & 1);
    }
}

这是我的结果:

输入:a

输出:

01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
01100001
  • 为什么这个函数是递归的?为什么不使用sizeof(unsigned char)*8 而不是CHAR_BIT?为什么numberOfOnes 是指针?我的问题多于答案。
  • 提示:这将是完全琐碎的与一个简单的for 循环有关。
  • 要么你的缩进被破坏了,要么你的编码风格很不清楚。无论如何,%c 不会给你一个字符串。
  • 在您的 for 循环中,您将 int 与 char 进行比较,循环中与 toBS 的比较读取为输入“a”的 97。 print(toBS) 运行了 98 次,这是它在您发布的输出中显示的次数

标签: c


【解决方案1】:

除了糟糕的格式之外,您的代码还有一些问题:

scanf("%c", &toBS);
// will only get one character, not a string.
// and there is no testing to see if scanf() actually worked.
for (i = 0; i < toBS + 1; i++) { // counts up to the ASCII character's value.
    print(toBS);
}
void print(char c) { // sole purpose is to add one parameter to recursive function call
printf("%d", value & 1); // all the machinery of 'printf()' to output either '0' or '1'

如果您按照自己的方式思考自己想要的东西,它可以变得更简单。

#include <stdio.h>
#include <limits.h>

void asBin( char c, int b ) {
    if( b ) {
        asBin( c, b-1 );
        putchar( '0' + (1&(c>>(CHAR_BIT-b))) );
        if( b == CHAR_BIT ) putchar( ' ' );
    }
}

int main() {
    for( char *str = "Hello!"; *str; str++ )
        asBin( *str, CHAR_BIT );
    putchar( '\n' );

    return 0;
}
01001000 01100101 01101100 01101100 01101111 00100001

如果你应用正确的方法(迭代)而不是递归,甚至更简单。

#include <stdio.h>
#include <limits.h>

int main() {
    for( char *str = "Hello!"; *str; str++ ) {
        for( int i = CHAR_BIT; i--;  )
            putchar( '0' + (1&(*str>>i)) );
        putchar( "\n "[str[1] != 0] );
    }
    return 0;
}

一旦基础到位,就可以重新安排事情。以下(递归)代码输出 7 位 ASCII 表的 3/4。

#include <stdio.h>
#include <stddef.h>

void asBin( uint8_t c, int b ) {
    if( --b ) asBin( (uint8_t)(c>>1), b );
    putchar( "01"[c&1] );
}

int main() {
    for( uint8_t cr = ' '; cr < ' '+32; cr++  ) {
        for( uint8_t cc = cr; cc < ' '+32+32+31; cc += 32 ) {
            printf( " %c ", cc );
            asBin( cc, 8 );
        }
        putchar( '\n' );
    }

    return 0;
}
   00100000 @ 01000000 ` 01100000
 ! 00100001 A 01000001 a 01100001
 " 00100010 B 01000010 b 01100010
 # 00100011 C 01000011 c 01100011
 $ 00100100 D 01000100 d 01100100
        /* omitted */
 9 00111001 Y 01011001 y 01111001
 : 00111010 Z 01011010 z 01111010
 ; 00111011 [ 01011011 { 01111011
 < 00111100 \ 01011100 | 01111100
 = 00111101 ] 01011101 } 01111101
 > 00111110 ^ 01011110 ~ 01111110
 ? 00111111 _ 01011111

【讨论】:

    猜你喜欢
    • 2019-07-24
    • 1970-01-01
    • 2012-01-03
    • 2020-05-18
    • 2014-06-14
    • 2020-01-14
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    相关资源
    最近更新 更多