c - C 程序将用户输入转换为整数,然后将整数转换为(HEX、Bin、Alpha)的问题

标签 c integer hex ascii scanf

#include <stdio.h>
#include <stdlib.h>

int main(void) {

/*holds the number to be converted between methods.*/
char convNumber;

/*storageInt */
int storageInt;

    puts("put text in here to convert to bin");
    while ((convNumber = getchar()) != '$') {

        storageInt = storageInt * 100 + convNumber;

    }

     printf("%d", &storageInt);

    return 0;
}

好的,上面是给我带来问题的代码。我是 C 语言新手,这是一个学校项目。

问题: 如何将带有分隔符“$”的用户输入转换为可以存储和打印或用于转换为十六进制或二进制的整数。页面底部是我的输出最终应该是什么样子的完整 View ,以给出所需输入类型的想法。

背景和任务信息:

尝试将用户输入打印为整数时,我不断收到意外的输出。我的目标是拥有 2 个整数 block ,代表 ASCII,按从 0 到 N 的顺序(N 是用户输入的末尾)。通过键入 $ 将用户输入指定为完成。我做错了什么?

当浏览程序时,选择“A”、“A”,最后选择“A$”($ 用于终止程序),我得到 2337412,而不是预期的 65 个 ascii 'A'。

所以请帮助我理解这一点,以便我在后续作业中做得更好(寻找解释)。提前致谢!

最终结果:

Welcome to the Coder!
Choose Input (H,A,B,Q): A
Choose Output(H,A,B): H
Enter your alpha input.
Hi! Bye! $
486921204279652120
Choose Input (H,A,B,Q): H
Choose Output(H,A,B): B
Enter your hex input.
486921204279652120$
010010000110100100100001001000000100001001111001011001010010000100100000
Choose Input (H,A,B,Q): B
Choose Output(H,A,B): A
Enter your bin input.

最佳答案

您正在打印storageInt的内存地址。仅在 scanf 中使用内存地址,其原因在您了解更多信息后会发现显而易见。字符串是一个异常(exception),但这不是字符串。

这样做:

printf("%d", storageInt);

关于c - C 程序将用户输入转换为整数,然后将整数转换为(HEX、Bin、Alpha)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27998550/

相关文章:

c++ - ptrace如何捕捉fork的系统调用?

c++ - 用于 Web 应用程序核心逻辑开发的 C/C++?

编译 "stress-ng"包的静态链接可执行文件

python - MAC地址到二进制位的转换

c - 带有 strcpy 函数的运行时错误 "access violation writing location "

c - 解析特定数字

C:反转 int 的递归函数

java - 如何从文本框中获取数字并乘以值

php - 用于匹配 CSS 十六进制颜色的正则表达式

java - 在 Java 中将不可打印字符打印为句号