简体   繁体   English

从数组 c 中读取 WORD

[英]Reading WORD from a array c

Is there a good way to parse 32 bit values out of a string?有没有一种从字符串中解析 32 位值的好方法? the following function does not throw the correct value and I don't know how to fix it.以下 function 没有抛出正确的值,我不知道如何修复它。 the value should be 170该值应为 170

unsigned int getvalue(const char *data, int offset)
{
  unsigned int payload = 0;
  for (unsigned char i = 0; i < 4; i++)
  {
    payload <<= 8;
    payload |= data[i + offset];
  }
  return payload;
}



int value = 0x00aa; 
unsigned char b1 = (value & 0xFF); 
unsigned char b2 = ((value >> 8) & 0xFF); 
unsigned char b3 = ((value >> 16) & 0xFF); 
unsigned char b4 = ((value >> 24) & 0xFF); 

int number = (b4 << 24) + (b3 << 16) + (b2 << 8) + b1;
printf("value is: %d\n", number); // value is: 170 correct


char payload[] = "000000aa";
int value2 = getvalue(payload, 0);

printf("value is: %dn", value2); // value is: 808464432n Not correct

if you want to convert C string containing hexadecimal number) to its integer value:如果要将包含十六进制数字的 C 字符串)转换为其 integer 值:

unsigned long long conv(const char *str)
{
    const char digits[] = "01234567890ABCDEF";
    unsigned long long result = 0;
    char *ref;

    while(*str)
    {
        result *= 16;
        ref = strchr(digits, toupper((unsigned char)*str));
        if(ref)
        {
            result += ref - digits;
        }
        else
        {
            /* error handling */
        }
        str++;
    }
    return result
}

The digits in the C string are ASCII representation of the character representing the letter or digit. C 字符串中的数字是表示字母或数字的字符的 ASCII 表示。 '0' is not represented in the char array by zero but 0x30. '0'在 char 数组中不是用零表示,而是用 0x30 表示。

Change your getvalue to将您的 getvalue 更改为

unsigned int getvalue(const char *data, int offset)
{
  unsigned int payload = 0;
  for (unsigned char i = 0; i < 8; i++)
  {
    payload <<= 4;
    if (data[i + offset] >= 'a') {
        payload |= (data[i + offset] - 97) + 10;
    } else if (data[i + offset] >= 'A') {
        payload |= (data[i + offset] - 65) + 10;
    } else {
        payload |= (data[i + offset] - 48);
    }
  }
  return payload;
}

Each hex value occupies 4 bits, hence << 4 Each char in the string is in its ASCII code, hence the subtraction.每个十六进制值占用 4 位,因此 << 4 字符串中的每个字符都在其 ASCII 代码中,因此是减法。 ASCII value of 'a' is 97, but the decimal value must be 10, so the expression will be 'a' - 97 + 10 'a' 的 ASCII 值是 97,但十进制值必须是 10,所以表达式将是 'a' - 97 + 10

There is no need to reinvent the wheel.没有必要重新发明轮子。 There is a library function to do this: strtol .有一个库 function 可以做到这一点: strtol

#include <stdlib.h>

unsigned int getvalue(const char *data, int offset)
{
    char *end;
    long payload = strtol(&data[offset],&end,16);
    if (*end  ||  payload < 0  ||  payload > UINT_MAX) {
        /* error handling */
    }
    return (unsigned int)payload;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM