typedef union
{
uint ui[4];
} md5hash;
void main(void)
{
int opt;
while ((opt = getopt(argc, argv, "c:t:s:h:")) != -1) {
switch (opt) {
case 'h':
hash = optarg;
break;
default: /* '?' */
exit(EXIT_FAILURE);
}
}
md5hash hash;
sscanf(hash, "%x%x%x%x", &hash.ui);
}
./program -h ffffffffffffffffffffffffffffffff
./program -h fffffffffffffffffffffffffffffffff
I want to do the above, but sscanf does not accept the md5sum properly...
我想做上面的事情,但是sscanf不能正确地接受md5sum ......
2 个解决方案
#1
5
- You seem to have two variables called hash, except one is implicit in your code.
- The
sscanf
statement attempts to readhash
back into itself, but obviously it will not find any hexadecimal digits. -
%x
may load a different sized integer in hexadecimal on different platforms because you have not specified any specific length to read for each field. - You are not taking into account machine endianness.
您似乎有两个名为hash的变量,除了一个隐含在您的代码中。
sscanf语句尝试将哈希读回自身,但显然它不会找到任何十六进制数字。
%x可能会在不同平台上以十六进制加载不同大小的整数,因为您没有为每个字段指定任何特定长度。
您没有考虑机器字节序。
If you do have a hexadecimal string, let's say in hashString
then you can probably try
如果你有一个十六进制字符串,让我们说在hashString然后你可以尝试
int fieldsScanned = sscanf (hashString, "%8x%8x%8x%8x", &hash.ui[0], &hash.ui[1], &hash.ui[2], &hash.ui[3]);
if (fieldsScanned == 4)
{
// MD5 sum is in hash variable.
}
#2
0
int
parse_hex(char *s, unsigned char *hex, int len)
{
int i, r = 0;
len *= 2;
for (i = 0; ; i++, s++)
{
if (*s == 0 && !(i & 1))
return i / 2;
if (i == len)
{
fprintf(stderr, "parsehex: string too long\n");
//exit(1);
}
if (*s >= '0' && *s <= '9')
r = (r << 4) | (*s - '0');
else if (*s >= 'a' && *s <= 'f')
r = (r << 4) | (*s - ('a' - 10));
else if (*s >= 'A' && *s <= 'F')
r = (r << 4) | (*s - ('a' - 10));
else
{
fprintf(stderr, "parsehex: bad string\n");
//exit(1);
}
if ((i & 1) != 0)
{
hex[i / 2] = r;
r = 0;
}
}
}
void
parse_md5(char *s, unsigned char *md5)
{
if (!*s)
{
memset(md5, 0, 16);
return;
}
if (parse_hex(s, md5, 16) != 16)
{
fprintf(stderr, "parsemd5: bad md5\n");
//exit(1);
}
}
actually, the sscanf() method above does not work, it reads the bytes in reverse order within the doublewords.
实际上,上面的sscanf()方法不起作用,它在双字内以相反的顺序读取字节。
#1
5
- You seem to have two variables called hash, except one is implicit in your code.
- The
sscanf
statement attempts to readhash
back into itself, but obviously it will not find any hexadecimal digits. -
%x
may load a different sized integer in hexadecimal on different platforms because you have not specified any specific length to read for each field. - You are not taking into account machine endianness.
您似乎有两个名为hash的变量,除了一个隐含在您的代码中。
sscanf语句尝试将哈希读回自身,但显然它不会找到任何十六进制数字。
%x可能会在不同平台上以十六进制加载不同大小的整数,因为您没有为每个字段指定任何特定长度。
您没有考虑机器字节序。
If you do have a hexadecimal string, let's say in hashString
then you can probably try
如果你有一个十六进制字符串,让我们说在hashString然后你可以尝试
int fieldsScanned = sscanf (hashString, "%8x%8x%8x%8x", &hash.ui[0], &hash.ui[1], &hash.ui[2], &hash.ui[3]);
if (fieldsScanned == 4)
{
// MD5 sum is in hash variable.
}
#2
0
int
parse_hex(char *s, unsigned char *hex, int len)
{
int i, r = 0;
len *= 2;
for (i = 0; ; i++, s++)
{
if (*s == 0 && !(i & 1))
return i / 2;
if (i == len)
{
fprintf(stderr, "parsehex: string too long\n");
//exit(1);
}
if (*s >= '0' && *s <= '9')
r = (r << 4) | (*s - '0');
else if (*s >= 'a' && *s <= 'f')
r = (r << 4) | (*s - ('a' - 10));
else if (*s >= 'A' && *s <= 'F')
r = (r << 4) | (*s - ('a' - 10));
else
{
fprintf(stderr, "parsehex: bad string\n");
//exit(1);
}
if ((i & 1) != 0)
{
hex[i / 2] = r;
r = 0;
}
}
}
void
parse_md5(char *s, unsigned char *md5)
{
if (!*s)
{
memset(md5, 0, 16);
return;
}
if (parse_hex(s, md5, 16) != 16)
{
fprintf(stderr, "parsemd5: bad md5\n");
//exit(1);
}
}
actually, the sscanf() method above does not work, it reads the bytes in reverse order within the doublewords.
实际上,上面的sscanf()方法不起作用,它在双字内以相反的顺序读取字节。