• 全部
  • 问答

我写了个“两个十六进制字符串异或后输出二进制结果”的C语言程序,但是整形数组存储实在太小了

zone971016 2021-03-23 10:36:32
https://paste.ubuntu.com/p/vc4PKrJxDq/

最开始的m和n数组是要异或的输入,再多一位就不行了,但是异或操作又只能在整形上操作,真的服了

...全文
110 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
引用 3 楼 forever74 的回复:
就是先算出来,查表输出即可。
大佬你可太强了! 受我一拜!
回复
forever74 03-24
就是先算出来,查表输出即可。
回复
forever74 03-24
如果你打算把两个字符数组按字面十六进制异或生成另一个字符数组, 那可以这样玩:

#include <stdio.h>
#include <string.h> 

int hexc2v(const char c)
{
	return (c >= '0'&&c <= '9') ? c - '0' : (c >= 'a'&&c <= 'f') ? c - 'a' + 10 : (c >= 'A'&&c <= 'F') ? c - 'A' + 10 : -1;
}
void hexstr_xor(char* k, const char* m, const char* n) //假设两个数组里面的字符串长度相同并且已经对齐
{
	static const char pre[16][16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','1','0','3','2','5','4','7','6','9','8','b','a','d','c','f','e',
							'2','3','0','1','6','7','4','5','a','b','8','9','e','f','c','d','3','2','1','0','7','6','5','4','b','a','9','8','f','e','d','c',
							'4','5','6','7','0','1','2','3','c','d','e','f','8','9','a','b','5','4','7','6','1','0','3','2','d','c','f','e','9','8','b','a',
							'6','7','4','5','2','3','0','1','e','f','c','d','a','b','8','9','7','6','5','4','3','2','1','0','f','e','d','c','b','a','9','8',
							'8','9','a','b','c','d','e','f','0','1','2','3','4','5','6','7','9','8','b','a','d','c','f','e','1','0','3','2','5','4','7','6',
							'a','b','8','9','e','f','c','d','2','3','0','1','6','7','4','5','b','a','9','8','f','e','d','c','3','2','1','0','7','6','5','4',
							'c','d','e','f','8','9','a','b','4','5','6','7','0','1','2','3','d','c','f','e','9','8','b','a','5','4','7','6','1','0','3','2',
							'e','f','c','d','a','b','8','9','6','7','4','5','2','3','0','1','f','e','d','c','b','a','9','8','7','6','5','4','3','2','1','0' };
	int i = 0;
	while (m[i])
	{
		k[i] = pre[hexc2v(m[i])][hexc2v(n[i])];
		i++;
	}
	k[i] = 0;
}

int main()
{
	char m[256] = "0123456789abcdef";
	char n[256] = "fedcba9876543210";
	char k[256] = { 0 };
	hexstr_xor(k, m, n);
	puts(k);
	return 0;
}

回复
发帖
C++ 语言
创建于2007-09-28

5.9w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2021-03-23 10:36
社区公告
暂无公告