鱼C论坛

 找回密码
 立即注册
查看: 2141|回复: 16

怎么样输出二进制不省略前面的零?

[复制链接]
发表于 2022-1-21 20:48:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
int main ()
{
        int i;
        char b[100];
       
        scanf("%d",&i);
        itoa (i,b,2);
        printf("%s\n",b);
       
       
        return 0;
}

比如1的二进制输出结果是1,怎么才能输出00000001这样的形式,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-21 21:00:02 | 显示全部楼层
这样可以吗
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int main ()
  5. {
  6.         int i,j,len;
  7.         char b[100];
  8.       
  9.         scanf("%d",&i);
  10.         itoa (i,b,2);
  11.         len = strlen(b);
  12.         for(j=0;j<(8 -len);j++)
  13.                 printf("0");
  14.         printf("%s\n",b);
  15.       
  16.       
  17.         return 0;
  18. }
复制代码
3
00000011

--------------------------------
Process exited after 0.5775 seconds with return value 0
请按任意键继续. . .
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-21 21:10:06 | 显示全部楼层
本帖最后由 翼是孤独 于 2022-1-22 00:38 编辑

自己写代码补全一下输出字符串就行

第一种方法:
把结果转成字符串,计算一下长度,前面拼接你要的0
比如十进制9,二进制1001,转成字符串4位,你要8位结果,前面就拼接4个0,结果:00001001

第二种方法:
你要8位的结果,原数就加2的8次方即256,转成二进制之后,转字符串,去掉最高位的1
如十进制9,二进制1001,9+256 = 265二进制为 1 0000 1001 ,转成字符串 100001001,去掉第一个1,就是结果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-21 22:43:12 | 显示全部楼层
  1. #include <stdio.h>

  2. int main()
  3. {
  4.     int n, buff[64] = {0};
  5.     scanf("%d", &n);
  6.     for(int i = 0; n; i++, n /= 2) buff[i] += n%2;
  7.     for(int i = 64; i > 0;) printf("%d", buff[--i]);
  8.     return 0;
  9. }
复制代码
输入/输出:
  1. 1234567890
  2. 0000000000000000000000000000000001001001100101100000001011010010
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-21 23:20:46 | 显示全部楼层

谢谢,看懂了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-21 23:24:16 | 显示全部楼层
翼是孤独 发表于 2022-1-21 21:10
自己写代码补全一下输出字符串就行

第一种方法:

第二种方法,能写个代码吗?理论看懂了,刚学习C,还不实现
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 00:08:16 | 显示全部楼层
本帖最后由 翼是孤独 于 2022-1-22 00:36 编辑
bz00 发表于 2022-1-21 23:24
第二种方法,能写个代码吗?理论看懂了,刚学习C,还不实现

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int main ()
  5. {
  6.         int i;
  7.         char b[100];
  8.         scanf("%d",&i);
  9.         itoa (i+256,b,2);
  10.         strcpy(b,b+1);
  11.         printf("%s\n",b);
  12.         return 0;
  13. }
复制代码


加一点提示:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int main ()
  5. {
  6.         int i,n;
  7.         char b[100];
  8.         printf("请输入需要需要转换的数字:");
  9.         scanf("%d",&i);

  10.         printf("请输入最终显示位数:");
  11.         scanf("%d",&n);
  12.         //i + 2^n 转为2进制字符串
  13.         itoa (i+(1<<n),b,2);
  14.         //去掉最高位的1
  15.         strcpy(b,b+1);
  16.         printf("最终结果为:%s\n",b);
  17.         return 0;
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 00:22:57 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 00:34:32 | 显示全部楼层
人造人 发表于 2022-1-22 00:22
https://segmentfault.com/q/1010000020075140?utm_source=tag-newest

没问题啊,strycpy(str1,str2) 是把str2 copy到str1
很明显你说的那种溢出错误是因为str2的长度比str1预留的位置大
而这里b+1左移一位后明显比b长度小,所以是不会有问题的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 00:38:53 | 显示全部楼层
翼是孤独 发表于 2022-1-22 00:34
没问题啊,strycpy(str1,str2) 是把str2 copy到str1
很明显你说的那种溢出错误是因为str2的长度比str1预 ...
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     char str[] = "hello";
  6.     strcpy(str, str + 1);
  7.     puts(str);
  8.     return 0;
  9. }
  10. $ gcc-debug -o main main.c
  11. $ ./main
  12. =================================================================
  13. ==606369==ERROR: AddressSanitizer: strcpy-param-overlap: memory ranges [0x7fff4e2911a0,0x7fff4e2911a5) and [0x7fff4e2911a1, 0x7fff4e2911a6) overlap
  14.     #0 0x7fc0d796ab37 in __interceptor_strcpy /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438
  15.     #1 0x55c7f91f7327 in main /tmp/main.c:6
  16.     #2 0x7fc0d6dffb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
  17.     #3 0x55c7f91f710d in _start (/tmp/main+0x110d)

  18. Address 0x7fff4e2911a0 is located in stack of thread T0 at offset 32 in frame
  19.     #0 0x55c7f91f71e8 in main /tmp/main.c:4

  20.   This frame has 1 object(s):
  21.     [32, 38) 'str' (line 5) <== Memory access at offset 32 is inside this variable
  22. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  23.       (longjmp and C++ exceptions *are* supported)
  24. Address 0x7fff4e2911a1 is located in stack of thread T0 at offset 33 in frame
  25.     #0 0x55c7f91f71e8 in main /tmp/main.c:4

  26.   This frame has 1 object(s):
  27.     [32, 38) 'str' (line 5) <== Memory access at offset 33 is inside this variable
  28. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  29.       (longjmp and C++ exceptions *are* supported)
  30. SUMMARY: AddressSanitizer: strcpy-param-overlap /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438 in __interceptor_strcpy
  31. ==606369==ABORTING
  32. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 00:53:18 | 显示全部楼层
翼是孤独 发表于 2022-1-22 00:34
没问题啊,strycpy(str1,str2) 是把str2 copy到str1
很明显你说的那种溢出错误是因为str2的长度比str1预 ...
  1. $ cat main.c
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>

  5. /*
  6. =============
  7. itoa

  8. Convert integer to string

  9. PARAMS:
  10. - value     A 64-bit number to convert
  11. - str       Destination buffer; should be 66 characters long for radix2, 24 - radix8, 22 - radix10, 18 - radix16.
  12. - radix     Radix must be in range -36 .. 36. Negative values used for signed numbers.
  13. =============
  14. */

  15. #include <stdbool.h>

  16. char* itoa (unsigned long long  value,  char str[],  int radix)
  17. {
  18.     char        buf [66];
  19.     char*       dest = buf + sizeof(buf);
  20.     //boolean     sign = false;
  21.     bool        sign = false;

  22.     if (value == 0) {
  23.         memcpy (str, "0", 2);
  24.         return str;
  25.     }

  26.     if (radix < 0) {
  27.         radix = -radix;
  28.         if ( (long long) value < 0) {
  29.             value = -value;
  30.             sign = true;
  31.         }
  32.     }

  33.     *--dest = '\0';

  34.     switch (radix)
  35.     {
  36.     case 16:
  37.         while (value) {
  38.             * --dest = '0' + (value & 0xF);
  39.             if (*dest > '9') *dest += 'A' - '9' - 1;
  40.             value >>= 4;
  41.         }
  42.         break;
  43.     case 10:
  44.         while (value) {
  45.             *--dest = '0' + (value % 10);
  46.             value /= 10;
  47.         }
  48.         break;

  49.     case 8:
  50.         while (value) {
  51.             *--dest = '0' + (value & 7);
  52.             value >>= 3;
  53.         }
  54.         break;

  55.     case 2:
  56.         while (value) {
  57.             *--dest = '0' + (value & 1);
  58.             value >>= 1;
  59.         }
  60.         break;

  61.     default:            // The slow version, but universal
  62.         while (value) {
  63.             *--dest = '0' + (value % radix);
  64.             if (*dest > '9') *dest += 'A' - '9' - 1;
  65.             value /= radix;
  66.         }
  67.         break;
  68.     }

  69.     if (sign) *--dest = '-';

  70.     memcpy (str, dest, buf +sizeof(buf) - dest);
  71.     return str;
  72. }

  73. int main ()
  74. {
  75.         int i,n;
  76.         char b[100];
  77.         printf("请输入需要需要转换的数字:");
  78.         scanf("%d",&i);

  79.         printf("请输入最终显示位数:");
  80.         scanf("%d",&n);
  81.         //i + 2^n 转为2进制字符串
  82.         itoa (i+(1<<n),b,2);
  83.         //去掉最高位的1
  84.         strcpy(b,b+1);
  85.         printf("最终结果为:%s\n",b);
  86.         return 0;
  87. }
  88. $ gcc-debug -o main main.c
  89. $ ./main
  90. 请输入需要需要转换的数字:13
  91. 请输入最终显示位数:16
  92. =================================================================
  93. ==610718==ERROR: AddressSanitizer: strcpy-param-overlap: memory ranges [0x7ffc5fe90030,0x7ffc5fe90041) and [0x7ffc5fe90031, 0x7ffc5fe90042) overlap
  94.     #0 0x7f3976bf9b37 in __interceptor_strcpy /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438
  95.     #1 0x5596879c9ea9 in main /tmp/main.c:99
  96.     #2 0x7f397608eb24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
  97.     #3 0x5596879c918d in _start (/tmp/main+0x218d)

  98. Address 0x7ffc5fe90030 is located in stack of thread T0 at offset 80 in frame
  99.     #0 0x5596879c9c4d in main /tmp/main.c:88

  100.   This frame has 3 object(s):
  101.     [48, 52) 'i' (line 89)
  102.     [64, 68) 'n' (line 89)
  103.     [80, 180) 'b' (line 90) <== Memory access at offset 80 is inside this variable
  104. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  105.       (longjmp and C++ exceptions *are* supported)
  106. Address 0x7ffc5fe90031 is located in stack of thread T0 at offset 81 in frame
  107.     #0 0x5596879c9c4d in main /tmp/main.c:88

  108.   This frame has 3 object(s):
  109.     [48, 52) 'i' (line 89)
  110.     [64, 68) 'n' (line 89)
  111.     [80, 180) 'b' (line 90) <== Memory access at offset 81 is inside this variable
  112. HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
  113.       (longjmp and C++ exceptions *are* supported)
  114. SUMMARY: AddressSanitizer: strcpy-param-overlap /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438 in __interceptor_strcpy
  115. ==610718==ABORTING
  116. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 01:12:43 | 显示全部楼层

你用的啥编译器啊,刚下了个c编译器试试没报错啊
001.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 01:15:11 | 显示全部楼层
翼是孤独 发表于 2022-1-22 01:12
你用的啥编译器啊,刚下了个c编译器试试没报错啊

带内存检查的gcc
  1. $ alias gcc-debug
  2. alias gcc-debug='gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer'
  3. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 01:26:21 | 显示全部楼层

老哥不会是竞赛大佬吧,这种内存级别我还是溜了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 01:26:39 | 显示全部楼层
翼是孤独 发表于 2022-1-22 01:12
你用的啥编译器啊,刚下了个c编译器试试没报错啊
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void) {
  5.     {
  6.         char str[] = "hello";
  7.         strcpy(str, str + 1);
  8.         puts(str);
  9.     }
  10.     {
  11.         char str[] = "abcde";
  12.         strcpy(str, str + 1);
  13.         puts(str);
  14.     }
  15.     {
  16.         char str[] = "abcdef";
  17.         strcpy(str, str + 1);
  18.         puts(str);
  19.     }
  20.     {
  21.         char str[] = "abcd";
  22.         strcpy(str, str + 1);
  23.         puts(str);
  24.     }
  25.     {
  26.         char str[] = "abc";
  27.         strcpy(str, str + 1);
  28.         puts(str);
  29.     }
  30.     return 0;
  31. }
  32. $ gcc -g -Wall -o main main.c
  33. $ ./main
  34. eloo
  35. bdee
  36. bceef
  37. bcd
  38. bc
  39. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 01:28:03 | 显示全部楼层
翼是孤独 发表于 2022-1-22 01:26
老哥不会是竞赛大佬吧,这种内存级别我还是溜了

不是,我自学的
我主修的 C/C++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-22 10:17:59 | 显示全部楼层
谢谢各位,都是高手,有些我消化下,有些我还消化不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 22:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表