Python中的Python的binascii.unhexlify函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python中的Python的binascii.unhexlify函数相关的知识,希望对你有一定的参考价值。
我正在构建一个程序,将输入视为裸MAC地址并将其转换为二进制字符串。我在嵌入式系统上这样做,所以没有STD。我一直在尝试类似于this question的东西但是在2天之后我没有取得任何成就,我对这些事情真的很糟糕。
我想要的是输出等于目标,考虑到这一点:
#include <stdio.h>
int main() {
const char* goal = "xaaxbbxccxddxeexff";
printf("Goal: %s
", goal);
char* input = "aabbccddeeff";
printf("Input: %s
", input);
char* output = NULL;
// Magic code here
if (output == goal) {
printf("Did work! Yay!");
} else {
printf("Did not work, keep trying");
}
}
谢谢,这是个人项目,我真的想完成它
答案
首先,你的比较应该使用strcmp
否则它总是错误的。
然后,我会通过2-char读取字符串2-char并将每个“数字”转换为其值(0-15),然后通过移位组合结果
#include <stdio.h>
#include <string.h>
// helper function to convert a char 0-9 or a-f to its decimal value (0-16)
// if something else is passed returns 0...
int a2v(char c)
{
if ((c>='0')&&(c<='9'))
{
return c-'0';
}
if ((c>='a')&&(c<='f'))
{
return c-'a'+10;
}
else return 0;
}
int main() {
const char* goal = "xaaxbbxccxddxeexff";
printf("Goal: %s
", goal);
const char* input = "aabbccddeeff";
int i;
char output[strlen(input)/2 + 1];
char *ptr = output;
for (i=0;i<strlen(input);i+=2)
{
*ptr++ = (a2v(input[i])<<4) + a2v(input[i]);
}
*ptr = ' ';
printf("Goal: %s
", output);
if (strcmp(output,goal)==0) {
printf("Did work! Yay!");
} else {
printf("Did not work, keep trying");
}
}
以上是关于Python中的Python的binascii.unhexlify函数的主要内容,如果未能解决你的问题,请参考以下文章