鱼C论坛

 找回密码
 立即注册
查看: 1615|回复: 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这样的形式,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-1-21 21:00:02 | 显示全部楼层
这样可以吗
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main ()
{
        int i,j,len;
        char b[100];
       
        scanf("%d",&i);
        itoa (i,b,2);
        len = strlen(b);
        for(j=0;j<(8 -len);j++)
                printf("0");
        printf("%s\n",b);
       
       
        return 0;
}
3
00000011

--------------------------------
Process exited after 0.5775 seconds with return value 0
请按任意键继续. . .
想知道小甲鱼最近在做啥?请访问 -> 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,就是结果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

int main()
{
    int n, buff[64] = {0};
    scanf("%d", &n);
    for(int i = 0; n; i++, n /= 2) buff[i] += n%2;
    for(int i = 64; i > 0;) printf("%d", buff[--i]);
    return 0;
}
输入/输出:
1234567890 
0000000000000000000000000000000001001001100101100000001011010010
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

谢谢,看懂了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

第一种方法:

第二种方法,能写个代码吗?理论看懂了,刚学习C,还不实现
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

        printf("请输入最终显示位数:");
        scanf("%d",&n);
        //i + 2^n 转为2进制字符串
        itoa (i+(1<<n),b,2);
        //去掉最高位的1
        strcpy(b,b+1);
        printf("最终结果为:%s\n",b);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 00:22:57 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> 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长度小,所以是不会有问题的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

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

  This frame has 1 object(s):
    [32, 38) 'str' (line 5) <== Memory access at offset 33 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: strcpy-param-overlap /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438 in __interceptor_strcpy
==606369==ABORTING
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

/*
=============
itoa

Convert integer to string

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

#include <stdbool.h>

char* itoa (unsigned long long  value,  char str[],  int radix)
{
    char        buf [66];
    char*       dest = buf + sizeof(buf);
    //boolean     sign = false;
    bool        sign = false;

    if (value == 0) {
        memcpy (str, "0", 2);
        return str;
    }

    if (radix < 0) {
        radix = -radix;
        if ( (long long) value < 0) {
            value = -value;
            sign = true;
        }
    }

    *--dest = '\0';

    switch (radix)
    {
    case 16:
        while (value) {
            * --dest = '0' + (value & 0xF);
            if (*dest > '9') *dest += 'A' - '9' - 1;
            value >>= 4;
        }
        break;
    case 10:
        while (value) {
            *--dest = '0' + (value % 10);
            value /= 10;
        }
        break;

    case 8:
        while (value) {
            *--dest = '0' + (value & 7);
            value >>= 3;
        }
        break;

    case 2:
        while (value) {
            *--dest = '0' + (value & 1);
            value >>= 1;
        }
        break;

    default:            // The slow version, but universal
        while (value) {
            *--dest = '0' + (value % radix);
            if (*dest > '9') *dest += 'A' - '9' - 1;
            value /= radix;
        }
        break;
    }

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

    memcpy (str, dest, buf +sizeof(buf) - dest);
    return str;
}

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

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

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

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

  This frame has 3 object(s):
    [48, 52) 'i' (line 89)
    [64, 68) 'n' (line 89)
    [80, 180) 'b' (line 90) <== Memory access at offset 81 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: strcpy-param-overlap /build/gcc/src/gcc/libsanitizer/asan/asan_interceptors.cpp:438 in __interceptor_strcpy
==610718==ABORTING
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

你用的啥编译器啊,刚下了个c编译器试试没报错啊
001.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

带内存检查的gcc
$ alias gcc-debug
alias gcc-debug='gcc -g -Wall -fsanitize=undefined -fsanitize=leak -fsanitize=address -fno-omit-frame-pointer'
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

老哥不会是竞赛大佬吧,这种内存级别我还是溜了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

int main(void) {
    {
        char str[] = "hello";
        strcpy(str, str + 1);
        puts(str);
    }
    {
        char str[] = "abcde";
        strcpy(str, str + 1);
        puts(str);
    }
    {
        char str[] = "abcdef";
        strcpy(str, str + 1);
        puts(str);
    }
    {
        char str[] = "abcd";
        strcpy(str, str + 1);
        puts(str);
    }
    {
        char str[] = "abc";
        strcpy(str, str + 1);
        puts(str);
    }
    return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
eloo
bdee
bceef
bcd
bc
$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

不是,我自学的
我主修的 C/C++
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-1-22 10:17:59 | 显示全部楼层
谢谢各位,都是高手,有些我消化下,有些我还消化不了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-4 00:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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