user-0 提问: 求助

回复:
user: user-1
content: 一行100000这么长？ 十六制转八不需要转成什么十进制之类，直接3个字节一读转成4个八进制输出

user: user-2
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-3
content: 我的思想是，先把16进制数转为2进制数，再把2进制数转为8进制数。

user: user-3
content: 仅供参考：  #include <stdio.h> #include <string.h> /* 给定n个十六进制正整数，输出它们对应的八进制数。 */ char *table1[16] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; char *table2[8] = { "000", "001", "010", "011", "100", "101", "110", "111" }; //转二进制，一位转四位 void toBinary(char **nums, int n) { 	int len, i, j, k, pos; 	for (i = 0; i < n; i++) 	{ 		len = strlen(nums[i]); 		j = 399998; 		for (k = len - 1; k >= 0; k--) 		{ 			if (nums[i][k] >= 'A' && nums[i][k] <= 'F') 				pos = nums[i][k] - 'A' + 10; 			else 				pos = nums[i][k] - '0'; 			strncpy(nums[i] + j, table1[pos], 4); 			j -= 4; 		} 		j += 4;  		memset(nums[i], '0', j); 	} } //转八进制，三位转一位 void toOctal(char **nums, int n) { 	int i, j, k, r, s; 	for (i = 0; i < n; i++) 	{ 		j = 0; 		k = 399999; 		while (nums[i][j] == '0') 			j++;  		s = 400001; 		while (k >= j - 3) 		{ 			for (r = 0; r < 8; r++) 			{ 				if (strncmp(nums[i] + k, table2[r], 3) == 0) 				{ 					nums[i][s--] = r + '0'; 					break; 				} 			} 			k -= 3; 		} 		s++; 		while (nums[i][s] == '0') 			s++; 		//打印 		puts(nums[i] + s); 	} } int main(void)    //14 { 	//读入数据 	int n; 	scanf("%d", &n);  	char **nums = malloc(sizeof(char*)*n); 	int i; 	for (i = 0; i < n; i++) 	{ 		nums[i] = malloc(sizeof(char) * 400003); 		memset(nums[i], 0, 400003); 		scanf("%s", nums[i]); 	}  	//先转二进制 	toBinary(nums, n);  	//再转八进制 	toOctal(nums, n);   	//释放内存 	for (i = 0; i < n; i++) 		free(nums[i]); 	free(nums);  	return 0; }   在vs下测试通过

user: user-4
content: 每个十六进制数长度不超过100000  你试了39, 再试试393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939