从十六进制获取 LSB 的值(C 代码)

发布于 2024-09-05 16:22:58 字数 1525 浏览 4 评论 0原文

我在 C 中有这样的代码:

unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

我需要提取它,以便最终值是:

0xa1df7

如果十六进制值至少有 1 个零,我只能提取并使其工作:

unsigned char const data[ ] = {0x0a, 0xd0, 0xf0, 0x07};

使用下面的代码:

for(int i = 0; i < SIZE; ++i)
{
    tmp = data[i];
    if ( (data[i] <= 0x0F) &&  (((data[i] & 0x0F) == 0) || (data[i] & 0xF0) == 0)) // one of the hex is zero
    {
        tmp = ((tmp << 4) >> 4) << N[i];
        std::cout << "foo: " << std::hex << tmp << ":" << std::endl;    

    }
    else if ((data[i] >= 0x0F) &&  (((data[i] & 0x0F) == 0) || (data[i] & 0xF0) == 0) )
    {
        tmp = (tmp >> 4) << N[i];
        std::cout << "bar: " << std::hex << tmp << ":" << std::endl;

    }
    else
    {
        std::cout << "result: " << std::hex << result << ":" << std::endl;
        std::cout << "tmp << 8: " << std::hex << (tmp << 8)<< ":" << std::endl;
        result = result | (tmp << 8);
        std::cout << "result |= (tmp << 8): " << std::hex << result << ":" << std::endl;
    }

    result |= tmp;
    std::cout << "boo: " << std::hex << result << ":" << std::endl;
}

看来最后一个 else {...} 块对我来说很麻烦。有什么想法吗?谢谢!

I've got a code like this in C:

unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

I need to extract it such that the final value is:

0xa1df7

I have only been able to extract and get it working if the hex values that have at least 1 zero:

unsigned char const data[ ] = {0x0a, 0xd0, 0xf0, 0x07};

using the code below:

for(int i = 0; i < SIZE; ++i)
{
    tmp = data[i];
    if ( (data[i] <= 0x0F) &&  (((data[i] & 0x0F) == 0) || (data[i] & 0xF0) == 0)) // one of the hex is zero
    {
        tmp = ((tmp << 4) >> 4) << N[i];
        std::cout << "foo: " << std::hex << tmp << ":" << std::endl;    

    }
    else if ((data[i] >= 0x0F) &&  (((data[i] & 0x0F) == 0) || (data[i] & 0xF0) == 0) )
    {
        tmp = (tmp >> 4) << N[i];
        std::cout << "bar: " << std::hex << tmp << ":" << std::endl;

    }
    else
    {
        std::cout << "result: " << std::hex << result << ":" << std::endl;
        std::cout << "tmp << 8: " << std::hex << (tmp << 8)<< ":" << std::endl;
        result = result | (tmp << 8);
        std::cout << "result |= (tmp << 8): " << std::hex << result << ":" << std::endl;
    }

    result |= tmp;
    std::cout << "boo: " << std::hex << result << ":" << std::endl;
}

It seems the last else {...} block is troublesome for me. Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左岸枫 2024-09-12 16:22:58
#include <stdio.h>
#include <stdlib.h>

unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

int main(int argc, char*argv[]){
    int i,r = 0;
    for(i=0; i<sizeof(data); i++){
        if(data[i] & 0xf0) r = (r<<4) + (data[i]>>4);
        if(data[i] & 0x0f) r = (r<<4) + (data[i]&0x0f);
    }
    printf("%x\n",r);
    return 0;
}

输出“a1df7”

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

unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

int main(int argc, char*argv[]){
    int i,r = 0;
    for(i=0; i<sizeof(data); i++){
        if(data[i] & 0xf0) r = (r<<4) + (data[i]>>4);
        if(data[i] & 0x0f) r = (r<<4) + (data[i]&0x0f);
    }
    printf("%x\n",r);
    return 0;
}

outputs "a1df7"

感情旳空白 2024-09-12 16:22:58

您最好先将其全部转换为十六进制字符串(“0a1df007”),然后删除所有零:)

You'd better convert it all to a hex string first ("0a1df007") and then delete all zeroes :)

唔猫 2024-09-12 16:22:58
 unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

 unsigned int value=0;
 for(int i=0; i<4; i++)
 {
     int nibble = (data[i] & 0xf0) >> 4;
     if(nibble > 0)
     {
          value<<=4;
          value += nibble;
     }
     nibble = data[i] & 0x0f;
     if(nibble > 0)
     {
          value<<=4;
          value += nibble;
     }
 }
 unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

 unsigned int value=0;
 for(int i=0; i<4; i++)
 {
     int nibble = (data[i] & 0xf0) >> 4;
     if(nibble > 0)
     {
          value<<=4;
          value += nibble;
     }
     nibble = data[i] & 0x0f;
     if(nibble > 0)
     {
          value<<=4;
          value += nibble;
     }
 }
太阳公公是暖光 2024-09-12 16:22:58
unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

嗯,但这看起来像是一个简单的网络字节顺序。

对于 32 位 int,请执行以下操作:

unsigned char *p = (unsigned char *)data;
int val = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];

对于 64 位值,执行类似操作。

PS 注意标志。在某些情况下,需要显式转换为有符号类型才能强制符号位扩展。

unsigned char const data[ ] = {0x0a, 0x1d, 0xf0, 0x07};

Uhm, but that looks like a plain network byte order.

For 32bit int, do something like:

unsigned char *p = (unsigned char *)data;
int val = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];

And similarly for 64bit values.

P.S. Be careful about sign. In some cases explicit conversion to a signed type is required to force sign bit extension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文