将十六进制字符串转换为数据

发布于 2024-09-01 23:31:27 字数 547 浏览 1 评论 0原文

我已经找到了很多不同的解决方案来解决这个问题,但并不是所有的解决方案都有效,而且其中很多看起来都有些老套和低效。基本上我有一串十六进制数据(即“55 AA 41 2A 00 94 55 AA BB BB 00 FF”),我想将其转换为原始数据。最好的方法是什么?

更新:Vicky 的解决方案对我来说非常有用,但我将其更改为使用中间没有空格的十六进制字符串,并稍微更改了样式。

int i = 0;
char *hexString = "55AA412A009455AABBBB00FF"
char *hexPtr = hexString;
unsigned int *result = calloc(strlen(hexString)/2 + 1, sizeof *result);

while (sscanf(hexPtr, "%02x", &result[i++])) {
    hexPtr += 2;
    if (hexPtr >= hexString + strlen(hexString)) break;
}

return result;

I have found a whole lot of different solutions to this problem, but not all of them work, and a lot of them seem somewhat hacky and inefficient. Basically I have a string of hexadecimal data (i.e. "55 AA 41 2A 00 94 55 AA BB BB 00 FF") which I would like to convert to raw data. What is the best way to do this?

UPDATE: Vicky's solution worked great for me, but I changed it to work with hexadecimal strings that don't have spaces in between and changed the style a bit.

int i = 0;
char *hexString = "55AA412A009455AABBBB00FF"
char *hexPtr = hexString;
unsigned int *result = calloc(strlen(hexString)/2 + 1, sizeof *result);

while (sscanf(hexPtr, "%02x", &result[i++])) {
    hexPtr += 2;
    if (hexPtr >= hexString + strlen(hexString)) break;
}

return result;

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

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

发布评论

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

评论(5

打小就很酷 2024-09-08 23:31:38
 /*
  * copyLeft (ButterC) All Right Changed :)
 */
 void _StackOverFlow(void *data) {

    unsigned char *ButterC = (unsigned char *) data;
    size_t ssize ,size = ssize = 0; 

    int i;

    for (i = 0; ButterC[i] != 0; i++)
    {
            if (ButterC[i] != 32)
                    size++;
    }

    unsigned int *resu = (unsigned int *) malloc((size / 2) + 1);


    while (*ButterC != 0 && resu != NULL && ssize < size) 
    {
            if (*ButterC == 32) 
                    ButterC++;
            else {
                    sscanf(ButterC,"%02x",&resu[ssize++]); ButterC += 2;
            }
    }
    // TEST RESULTE TO => (resu) 
    for (i = 0; i < ssize; i++)
    {
            printf("%02X ",resu[i] & 0xff);
    }

    puts(""); // NEWLINE  
}

int _start() {

    char data[] = "FF 00FF           01";
    _StackOverFlow(data);

    
    _exit(0);
}
 /*
  * copyLeft (ButterC) All Right Changed :)
 */
 void _StackOverFlow(void *data) {

    unsigned char *ButterC = (unsigned char *) data;
    size_t ssize ,size = ssize = 0; 

    int i;

    for (i = 0; ButterC[i] != 0; i++)
    {
            if (ButterC[i] != 32)
                    size++;
    }

    unsigned int *resu = (unsigned int *) malloc((size / 2) + 1);


    while (*ButterC != 0 && resu != NULL && ssize < size) 
    {
            if (*ButterC == 32) 
                    ButterC++;
            else {
                    sscanf(ButterC,"%02x",&resu[ssize++]); ButterC += 2;
            }
    }
    // TEST RESULTE TO => (resu) 
    for (i = 0; i < ssize; i++)
    {
            printf("%02X ",resu[i] & 0xff);
    }

    puts(""); // NEWLINE  
}

int _start() {

    char data[] = "FF 00FF           01";
    _StackOverFlow(data);

    
    _exit(0);
}
二手情话 2024-09-08 23:31:37
while(input[i]!='\0'){        
    *(temp++) = ((input[i]>='0' && input[i]<='9')?(input[i]-'0'):(input[i]-'a'+10))*16 +( (input[i+1]>='0' && input[i]<='9')?(input[i+1]-'0'):(input[i+1]-'a'+10));
         i+=2;
}
while(input[i]!='\0'){        
    *(temp++) = ((input[i]>='0' && input[i]<='9')?(input[i]-'0'):(input[i]-'a'+10))*16 +( (input[i+1]>='0' && input[i]<='9')?(input[i+1]-'0'):(input[i+1]-'a'+10));
         i+=2;
}
终止放荡 2024-09-08 23:31:35
#define GETBITS(a) (a < 'A' ? a - '0' : toupper(a) - 'A' + 10)

char *cur;
char data;
for (cur = buffer; *cur; cur++) {
    data = GETBITS(cur[0]) << 4 + GETBITS(cur[1]);
    cur += 2;
    printf("%c", data);
}
printf("\n");
#define GETBITS(a) (a < 'A' ? a - '0' : toupper(a) - 'A' + 10)

char *cur;
char data;
for (cur = buffer; *cur; cur++) {
    data = GETBITS(cur[0]) << 4 + GETBITS(cur[1]);
    cur += 2;
    printf("%c", data);
}
printf("\n");
时光病人 2024-09-08 23:31:31

您是否尝试过 sscanfscanf?该函数处理十六进制值并返回“原始数据”。

Did you try sscanf or scanf? This function processes hexadecimal values to and returns "raw data".

喜你已久 2024-09-08 23:31:30

字符串的长度总是相同吗?

如果是:

char *buf = "55 AA 41 2A 00 94 55 AA BB BB 00 FF";
sscanf(buf, "%x %x %x [repeat as many times as necessary]", &a, &b, &c [etc]);

如果不是:

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

int main (int argc, char ** argv)
{
    char *buf = "55 AA 41 2A 00 94 55 AA BB BB 00 FF";
    char *p = buf;
    int i = 0, j;
    unsigned int *result = calloc(strlen(buf)/3 + 1 * sizeof(int), 1);

    if (result)
    {
        while (sscanf(p, "%02x", &result[i++]))
        {
             p += 3;
            if (p > buf + strlen(buf))
            {
             break;
            }
        }

        printf("%s\n", buf);

        for (j = 0; j < i; j++)
        {
            printf("%02X ", result[j]);
        }

        printf("\n");
    }
}

Is the string always the same length?

If so:

char *buf = "55 AA 41 2A 00 94 55 AA BB BB 00 FF";
sscanf(buf, "%x %x %x [repeat as many times as necessary]", &a, &b, &c [etc]);

If not:

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

int main (int argc, char ** argv)
{
    char *buf = "55 AA 41 2A 00 94 55 AA BB BB 00 FF";
    char *p = buf;
    int i = 0, j;
    unsigned int *result = calloc(strlen(buf)/3 + 1 * sizeof(int), 1);

    if (result)
    {
        while (sscanf(p, "%02x", &result[i++]))
        {
             p += 3;
            if (p > buf + strlen(buf))
            {
             break;
            }
        }

        printf("%s\n", buf);

        for (j = 0; j < i; j++)
        {
            printf("%02X ", result[j]);
        }

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