读取十六进制字符串到buf [英] Read hex string to a buf

查看:102
本文介绍了读取十六进制字符串到buf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


新手问题:我有一串半字节(比如2b11cc4f5db20cd5)

可以作为


#define DATA 0x2b11cc4f5db20cd5或

#define DATA" 2b11cc4f5db20cd5"


需要将此读入uint8数组buf,以便每个字节

表示一个十六进制数字(由DATA中的2个半字节组成)。你觉得什么是最好的方法?

谢谢你的帮助。

Hello,

Newbie question: I have a string of nibbles (say 2b11cc4f5db20cd5)
that can be put as

#define DATA 0x2b11cc4f5db20cd5 or
#define DATA "2b11cc4f5db20cd5"

Need to read this into a uint8 array buf such that each byte
represents a hex digit (formed out of 2 nibbles in DATA). What do you
think is the best way to do this?

Thanks for your help.

推荐答案

cp ********* @ gmail.com 写道:

你好,


新手问题:我有一串半字节(比如说2b11cc4f5db20cd5)

可以作为


#define DATA 0x2b11cc4f5db20cd5或

#define DATA" 2b11cc4f5db20cd5"

需要将其读入uint8数组buf,使得每个字节

表示一个十六进制数字(由DATA中的2个半字节组成)。你是什​​么?b $ b认为这是最好的方法吗?
Hello,

Newbie question: I have a string of nibbles (say 2b11cc4f5db20cd5)
that can be put as

#define DATA 0x2b11cc4f5db20cd5 or
#define DATA "2b11cc4f5db20cd5"

Need to read this into a uint8 array buf such that each byte
represents a hex digit (formed out of 2 nibbles in DATA). What do you
think is the best way to do this?



除非你的系统有一个可以容纳0x2b11cc4f5db20cd5的整数类型
或者你必须处理的最大值是,你的

唯一可行的选择。


-

Ian Collins。

Unless your system has an integer type that can hold 0x2b11cc4f5db20cd5
or whatever the biggest value you have to process is, the string is your
only viable option.

--
Ian Collins.


cp **** *****@gmail.com 写道:
cp*********@gmail.com wrote:

你好,


新手问题:我有一串半字节(比如2b11cc4f5db20cd5)

可以作为


#define DATA 0x2b11cc4f5db20cd5或

#define DATA" ; 2b11cc4f5db20cd5"


需要将其读入uint8数组buf,使得每个字节

代表一个十六进制数字(由DATA中的2个半字节组成)。你是什​​么?b $ b认为这是最好的方法吗?


感谢您的帮助。
Hello,

Newbie question: I have a string of nibbles (say 2b11cc4f5db20cd5)
that can be put as

#define DATA 0x2b11cc4f5db20cd5 or
#define DATA "2b11cc4f5db20cd5"

Need to read this into a uint8 array buf such that each byte
represents a hex digit (formed out of 2 nibbles in DATA). What do you
think is the best way to do this?

Thanks for your help.



我可以想出100种不同的方法,这取决于你想要用strtol,snprintf等获得的
如果您不想使用任何库

函数进行实际的十六进制工作:


/ *

* deadbeef.c:将十六进制字节转换为无符号字符的愚蠢代码

*输入应该是偶数个字符,否则你会炒掉你的cpu缓存。

* /

#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include< unistd.h>


const size_t minl = 2;

const size_t nibl = 4;

const char delim [2] =" 0x";

const char set [2] [16] = {" 0123456789abcdef"," 0123456789ABCDEF" };

unsigned char lookup [2] [256];


int main(int argc,char ** argv)

{

size_t i,l,n;

char * q;

unsigned char * p;


if(argc< 2)返回-1;

if((l = strlen((q = argv [1])))< minl ||(l& 0x01 ))

返回-1;


for(i = sizeof set; i - ;){

lookup [0 ] [(size_t)set [0] [i]] = i;

lookup [0] [(size_t)set [1] [i]] = i;

lookup [1] [i] = set [0] [i];

}


if(memcmp(q,delim,minl)== 0){

q + = minl; l - = minl;

}


if((p = malloc(sizeof * p * l / 2))== NULL)

返回-1;


for(i = l; i;){

n = lookup [0] [(size_t)q [ - -i]]<< nibl * 0;

n + = lookup [0] [(size_t)q [ - i]]<< nibl * 1;

p [i / 2] = n;

}


for(l / = 2; i< ; l; i ++){

n = p [i];

fprintf(stdout," p [%2.2u] == 0x%c%c%3.3u \ n",

i,

lookup [1] [(n& 0xf0)> nibl * 1],

lookup [ 1] [(n& 0x0f)> nibl * 0],

n);

}


免费(p );


返回0;

}

I can think of about a 100 different ways to do it depending on how goofy you
want to get with strtol, snprintf, etc. If you don''t want to use any library
functions for the actual hex work:

/*
* deadbeef.c: goofy code to convert hex bytes to unsigned char
* input should be an even number of characters or you''ll fry your cpu cache.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

const size_t minl = 2;
const size_t nibl = 4;
const char delim[2] = "0x";
const char set[2][16] = { "0123456789abcdef", "0123456789ABCDEF" };
unsigned char lookup[2][256];

int main(int argc, char **argv)
{
size_t i, l, n;
char *q;
unsigned char *p;

if (argc < 2) return -1;
if ((l = strlen((q = argv[1]))) < minl || (l & 0x01))
return -1;

for (i = sizeof set; i--; ) {
lookup[0][(size_t)set[0][i]] = i;
lookup[0][(size_t)set[1][i]] = i;
lookup[1][i] = set[0][i];
}

if (memcmp(q, delim, minl) == 0) {
q += minl; l -= minl;
}

if ((p = malloc(sizeof *p * l / 2)) == NULL)
return -1;

for (i = l; i; ) {
n = lookup[0][(size_t)q[--i]] << nibl * 0;
n += lookup[0][(size_t)q[--i]] << nibl * 1;
p[i / 2] = n;
}

for (l /= 2; i < l; i++) {
n = p[i];
fprintf(stdout, "p[%2.2u] == 0x%c%c %3.3u\n",
i,
lookup[1][(n & 0xf0) >nibl * 1],
lookup[1][(n & 0x0f) >nibl * 0],
n);
}

free(p);

return 0;
}


cc -O3 -W -Wall -Werror -pedantic -o hex2dec hex2dec.c
cc -O3 -W -Wall -Werror -pedantic -o hex2dec hex2dec.c


这篇关于读取十六进制字符串到buf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆