user-0 提问: 二进制字符问题

回复:
user: user-1
content: 修改成  str3 = int_to_bin((n1 & n2), str3); 	show_bin(str3);  也可以正常显示。原因是:       char * int_to_bin(int n, char *ps)       这个函数看着是传递了一个指针，其实本质还是形参的值传递形式，ps实际是实参的一个拷备，只不过ps和实参指向的是同一个地址，然后你在函数内部执行       ps = (char*)malloc(sizeof(char) * size + 1); 后，其实就改变了ps所指向的地址，但是不会改变实参的地址，所以你在此后所做的操作都是针对改变指向后的ps的操作，没有影响到实参，因此在函数执行后实参还是调用前的状态。

user: user-0
content: 谢谢各位，总算是用最开始的方法，写完整了。 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <limits.h> #include <ctype.h> #define Length 9  void show_bin(const char *); char * int_to_bin(int); char * int_to_bin111(int, char *); int bin_to_int(char *); int _tmain(int argc, char * argv[]) { 	char str1[Length] = ""; 	char str2[Length] = ""; 	const static int size = CHAR_BIT * sizeof(int); 	//char bin_str[CHAR_BIT * sizeof(int) + 1]; 	//char * str3 = (char*)malloc(sizeof(char) * Length + 1); 	char * bin_str22 = (char*)malloc(sizeof(char) * size + 1);  	int n1 = 0,  n2 = 0; 	printf("Please enter str1:"); 	scanf("%s", str1); 	while (getchar() != '\n') 		continue; 	printf("Please enter str2:"); 	scanf("%s", str2); 	while (getchar() != '\n') 		continue; 	n1 = bin_to_int(str1); 	n2 = bin_to_int(str2); 	printf("n1 is %d. n2 is %d.\n", n1, n2); 	printf("Operation & result is "); 	//str3 = int_to_bin((n1 & n2));//int_to_bin((n1 & n2), str3); 	int_to_bin111((n1 & n2), bin_str22); 	//show_bin(str3); 	//printf("********* & result is "); 	show_bin(bin_str22); 	printf("\n");  	printf("Operation | result is "); 	//str3 = int_to_bin((n1 | n2)); 	int_to_bin111((n1 | n2), bin_str22); 	//show_bin(str3); 	//printf("********* | result is "); 	show_bin(bin_str22); 	printf("\n");  	printf("Operation ^ result is "); 	//str3 = int_to_bin((n1 ^ n2)); 	int_to_bin111((n1 ^ n2), bin_str22); 	//show_bin(str3); 	//printf("********* ^ result is "); 	show_bin(bin_str22); 	printf("\n");  	//free(str3); 	free(bin_str22);  	system("PAUSE"); 	return 0; } char * int_to_bin(int n)//char * int_to_bin(int n, char *) { 	int i = 0; 	const static int size = CHAR_BIT * sizeof(int); 	char * ps = (char*)malloc(sizeof(char) * size + 1);	 	for (i = size - 1; i >= 0; i--, n >>= 1) 	{ 		ps[i] = (char)((01 & n) + '0'); 	} 	ps[size] = '\0'; 	return ps; } char * int_to_bin111(int n,  char * ps)//char * int_to_bin(int n, char *) { 	int i = 0; 	const static int size = CHAR_BIT * sizeof(int); 	//char * ps = (char*)malloc(sizeof(char) * size + 1); 	for (i = size - 1; i >= 0; i--, n >>= 1) 	{ 		ps[i] = (char)((01 & n) + '0'); 	} 	ps[size] = '\0'; 	return ps; } int bin_to_int(char *ps) { 	size_t length = 0; 	length = strlen(ps); 	int value = 0, cheng2 = 1; 	for (size_t i = 0; i < length; i++) 	{ 		cheng2 = 1; 		for (size_t j = 0; j < i; j++) 		{ 			cheng2 *= 2; 		} 		value += cheng2 * (int)(ps[length - i - 1] - '0'); 	} 	return value; } void show_bin(const char * str) { 	int i = 0; 	while (str[i]) 	{ 		putchar(str[i]); 		if (++i % 4 == 0 && str[i]) 		{ 			putchar(' '); 		} 	} 	printf("\n"); }

user: user-2
content: 整个代码只需要两处改动即可： ①在main函数中 str3 = (char*)malloc(sizeof(int) * Length + 1);//修改char为int memset(str3, 0, sizeof(int) * Length+1);//添加这一行  改动的意义是int_to_bin会返回一个32长度的char数组，而你str3只分配8字节长度，去接收肯定是不对的，其次分配内存空间后最好能初始化为0 ②int_to_bin函数中 屏蔽代码ps = (char*)malloc(sizeof(char) * size + 1);  改动的意义是我在①中添加的代码已经保证此时的ps集成了str3的初始化，重新初始化会产生错误  这些错误都是我加断点一步步找出来的，希望程序错误时，多用断点

user: user-0
content: 重新看了下书上写的。改了一下，char * int_to_bin(int, char *);这个函数的定义还是加上了char *试的，就是在主函数调用的时候，把之前的指针改成了字符串数组的模式。就可以了。但是不明白用指针的形式怎么就不行了。。。 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <limits.h> #include <ctype.h> #define Length 9  void show_bin(const char *); char * int_to_bin(int); char * int_to_bin111(int, char *);  int bin_to_int(char *); int _tmain(int argc, char * argv[]) { 	char str1[Length] = ""; 	char str2[Length] = ""; 	//const static int size = CHAR_BIT * sizeof(int); 	char * str3; 	char bin_str[CHAR_BIT * sizeof(int) + 1]; 	str3 = (char*)malloc(sizeof(char) * Length + 1); 	int n1 = 0,  n2 = 0; 	printf("Please enter str1:"); 	scanf("%s", str1); 	while (getchar() != '\n') 		continue; 	printf("Please enter str2:"); 	scanf("%s", str2); 	while (getchar() != '\n') 		continue; 	n1 = bin_to_int(str1); 	n2 = bin_to_int(str2); 	printf("n1 is %d. n2 is %d.\n", n1, n2); 	printf("Operation & result is "); 	str3 = int_to_bin((n1 & n2)); 	int_to_bin111((n1 & n2), bin_str); 	show_bin(str3); 	printf("********* & result is "); 	show_bin(bin_str); 	printf("\n");  	printf("Operation | result is "); 	str3 = int_to_bin((n1 | n2)); 	int_to_bin111((n1 | n2), bin_str); 	show_bin(str3); 	printf("********* | result is "); 	show_bin(bin_str); 	printf("\n");  	printf("Operation ^ result is "); 	str3 = int_to_bin((n1 ^ n2)); 	int_to_bin111((n1 ^ n2), bin_str); 	show_bin(str3); 	printf("********* ^ result is "); 	show_bin(bin_str); 	printf("\n");  	free(str3);  	system("PAUSE"); 	return 0; } char * int_to_bin(int n)//char * int_to_bin(int n, char *) { 	int i = 0; 	const static int size = CHAR_BIT * sizeof(int); 	char * ps = (char*)malloc(sizeof(char) * size + 1);	 	for (i = size - 1; i >= 0; i--, n >>= 1) 	{ 		ps[i] = (char)((01 & n) + '0'); 	} 	ps[size] = '\0'; 	return ps; } char * int_to_bin111(int n,  char * ps) { 	int i = 0; 	const static int size = CHAR_BIT * sizeof(int); 	//char * ps = (char*)malloc(sizeof(char) * size + 1); 	for (i = size - 1; i >= 0; i--, n >>= 1) 	{ 		ps[i] = (char)((01 & n) + '0'); 	} 	ps[size] = '\0'; 	return ps; } int bin_to_int(char *ps) { 	size_t length = 0; 	length = strlen(ps); 	int value = 0, cheng2 = 1; 	for (size_t i = 0; i < length; i++) 	{ 		cheng2 = 1; 		for (size_t j = 0; j < i; j++) 		{ 			cheng2 *= 2; 		} 		value += cheng2 * (int)(ps[length - i - 1] - '0'); 	} 	return value; } void show_bin(const char * str) { 	int i = 0; 	while (str[i]) 	{ 		putchar(str[i]); 		if (++i % 4 == 0 && str[i]) 		{ 			putchar(' '); 		} 	} 	printf("\n"); }

user: user-0
content: 引用 2 楼 user-4 的回复:供参考： int_to_bin声明改成 char * int_to_bin(int); str1和str2改成      char str1[Length+1] = "";     char str2[Length+1] = ""; int_to_bin函数改成： char * int_to_bin(int n) {     int i = 0;     const static int size = CHAR_BIT * sizeof(int);     char* ps = (char*)malloc(sizeof(char) * size + 1);     //memset(ps,0,sizeof(char)*(size+1));     for (i = size - 1; i >= 0; i--, n >>= 1)     {         ps[i] = ((01 & n)+'0');     }     ps[size] = '\0';     return ps; } 结果如下：   你的书上代码这么写，然后也没解释为什么这么写么？为什么数组形式就可以，指针形式就不行？想不明白。

user: user-3
content: 仅供参考：#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() {     int i,v;     char bs[33];     char b[33];     char hs[9];     char h[9];     char s[4];     char *e;  // 十进制整数转二进制串；     i=1024;     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("i=%d,bs=%s\n",i,bs); // 十进制整数转十六进制串；     i=1024;     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("i=%d,hs=%s\n",i,hs); // 十六进制字符串转成十进制数     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     printf("hs=%s,i=%d\n",hs,i); // 二进制字符串转化为十六进制字符串；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("bs=%s,hs=%s\n",bs,hs); // 二进制字符串转化为十进制数；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     printf("bs=%s,i=%d\n",bs,i); // 十六进制字符串转成二进制串     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("hs=%s,bs=%s\n",hs,bs); // ASC\GBK字符串转十六进制串     strcpy(s,"a汉");     i=0;     while (1) {         if (0==s[i]) break;         sprintf(hs+i*2,"%02X",(unsigned char)s[i]);         i++;     }     setlocale(LC_ALL,"chs");     printf("s=%s,hs=%s\n",s,hs); // 十六进制字符串转成汉字（GBK）及字符(ASC)     strcpy(hs,"61BABA");     i=0;     while (1) {         if (1!=sscanf(hs+i*2,"%2x",&v)) break;         s[i]=(char)v;         i++;     }     s[i]=0;     printf("hs=%s,s=%s\n",hs,s);      return 0;  } //i=1024,bs=00000000000000000000010000000000 //i=1024,hs=00000400 //hs=00000400,i=1024 //bs=00000000000000000000010000000000,hs=00000400 //bs=00000000000000000000010000000000,i=1024 //hs=00000400,bs=00000000000000000000010000000000 //s=a汉,hs=61BABA //hs=61BABA,s=a汉

user: user-4
content: 供参考： int_to_bin声明改成 char * int_to_bin(int); str1和str2改成      char str1[Length+1] = "";     char str2[Length+1] = ""; int_to_bin函数改成： char * int_to_bin(int n) {     int i = 0;     const static int size = CHAR_BIT * sizeof(int);     char* ps = (char*)malloc(sizeof(char) * size + 1);     //memset(ps,0,sizeof(char)*(size+1));     for (i = size - 1; i >= 0; i--, n >>= 1)     {         ps[i] = ((01 & n)+'0');     }     ps[size] = '\0';     return ps; } 结果如下：   你的书上代码这么写，然后也没解释为什么这么写么？

user: user-0
content: 为什么输出的结果是这样乱码的？ int_to_bin这个函数哪儿写的有问题？这个是照着书上写的啊。