怎么样输出二进制不省略前面的零?
#include<stdio.h>#include<stdlib.h>
int main ()
{
int i;
char b;
scanf("%d",&i);
itoa (i,b,2);
printf("%s\n",b);
return 0;
}
比如1的二进制输出结果是1,怎么才能输出00000001这样的形式,谢谢 这样可以吗
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main ()
{
int i,j,len;
char b;
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
请按任意键继续. . . 本帖最后由 翼是孤独 于 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,就是结果
#include <stdio.h>
int main()
{
int n, buff = {0};
scanf("%d", &n);
for(int i = 0; n; i++, n /= 2) buff += n%2;
for(int i = 64; i > 0;) printf("%d", buff[--i]);
return 0;
}输入/输出:1234567890
0000000000000000000000000000000001001001100101100000001011010010 大马强 发表于 2022-1-21 21:00
这样可以吗
谢谢,看懂了{:5_109:} 翼是孤独 发表于 2022-1-21 21:10
自己写代码补全一下输出字符串就行
第一种方法:
第二种方法,能写个代码吗?理论看懂了,刚学习C,还不实现 本帖最后由 翼是孤独 于 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;
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;
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;
} 翼是孤独 发表于 2022-1-22 00:08
#include
#include
#include
https://segmentfault.com/q/1010000020075140?utm_source=tag-newest 人造人 发表于 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长度小,所以是不会有问题的 翼是孤独 发表于 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
$ 翼是孤独 发表于 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 longvalue,char str[],int radix)
{
char buf ;
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;
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
$ 人造人 发表于 2022-1-22 00:38
你用的啥编译器啊,刚下了个c编译器试试没报错啊 翼是孤独 发表于 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'
$ 人造人 发表于 2022-1-22 01:15
带内存检查的gcc
老哥不会是竞赛大佬吧,这种内存级别我还是溜了{:10_266:} 翼是孤独 发表于 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
$ 翼是孤独 发表于 2022-1-22 01:26
老哥不会是竞赛大佬吧,这种内存级别我还是溜了
不是,我自学的
我主修的 C/C++
谢谢各位,都是高手,有些我消化下,有些我还消化不了{:5_109:}
页:
[1]