gpt4 book ai didi

c - 单元测试 : Compare Expected Result with hexadezimal

转载 作者:行者123 更新时间:2023-11-30 19:12:02 25 4
gpt4 key购买 nike

我想将一个十六进制与一个十六进制进行比较并显示是否相同。例如:

decimal = 255 
ExpResult = FF
Hex[x] = FF --> true

最佳答案

我给你写了一个程序,它会比较你的程序的两个参数,并输出它们是否匹配......希望它会有所帮助;)

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

static char *malloc_hexa(unsigned int num)
{
int len = 0;

if (num == 0)
len = 1;
while (num > 0)
{
num /= 16;
len++;
}
return (malloc(sizeof(char) * (len + 1)));
}

int main(int ac, char **av)
{
char *tmp = NULL;
unsigned int num = 0;

if (ac != 3)
{
printf("Use: ./test [number] [Hexa]\n");
return (1);
}
num = (unsigned int)atoi(av[1]);
if (!(tmp = malloc_hexa(num)))
return (1);
if (sprintf(tmp, "%X", num) <= 0)
return (1);
if (!strcmp(tmp, av[2]))
printf("match: %s / %s\n", tmp, av[2]);
else
printf("no match: %s / %s\n", tmp, av[2]);
free(tmp);
return (0);
}

关于c - 单元测试 : Compare Expected Result with hexadezimal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37991855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com