声明长十六进制字符串? [英] declaring long hex strings?

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

问题描述

您好,我有以下问题:


在C中声明十六进制数据时,通常会执行以下操作:

const char [] = { 0xde,0xad,0xbe,0xef,0x01,0x02,0x03,0x04 ......};


这是非常冗长的,使得源难以阅读。


另一种选择是

const char [] =" deadbeef01020304" ;;


但这会使每个nybble的存储要求翻倍将
转换为ASCII字符。


一些旧的汇编程序具有HEX指令,用于声明大块的原始数据。我的问题是:有没有办法在C中做到这一点?过多的

预处理器杂技是可以的,只要它们在gcc中工作...


我所拥有的十六进制数据是几千个字符串,如:

CRC(b746de6d)SHA1(ea69f87f84ded1f0a66457af24cbc692e5ff67e3)


实际上,我只想要一个预处理器宏来执行一个简单的正则表达式,例如

s /([a -fA-F0-9] [a-fA-F0-9])/ 0x\1,/是可能的吗?

Hello, I have the following problem:

when declaring hex data in C, you typically do something like:
const char[] = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };

This is quite verbose and makes the source hard to read.

An alternative is something like
const char[] = "deadbeef01020304";

but this doubles the storage requirements since each nybble is
converted to an ASCII char.

Some old assemblers have HEX directives for declaring large chunks of
raw data. My question is: is there any way to do this in C? Excessive
preprocessor acrobatics are OK as long as they work in gcc...

The hex data I have is several thousand strings like:
CRC(b746de6d) SHA1(ea69f87f84ded1f0a66457af24cbc692e5ff67e3)

Really, I just want a preprocessor macro that does a trivial regex like
s/([a-fA-F0-9][a-fA-F0-9])/0x\1,/ is that possible?

推荐答案

ar******@mac.com 写道:
您好,我有以下问题:

在C语句中声明十六进制数据时,通常会执行以下操作:
const char [] = {0xde,0xad,0xbe,0xef,0x01,0x02,0x03 ,0x04 ......};


更好

const unsigned char [] = ....

0x **是无符号整数值和转换如果char被签名导致未定义的行为,则char可能导致
溢出。

这非常冗长并且使源难以阅读。


确实。


另一种选择是
const char [] =" deadbeef01020304";
<但是这会使存储要求加倍,因为每个nybble都被转换为ASCII字符。


不。 C没有指定任何关于字符编码的内容;

我们也可以使用EBCDIC或其他任何东西。 4位半字节

由一个具有CHAR_BIT位的字符表示。


一些旧的汇编程序有HEX指令用于声明大块的原始数据。我的问题是:有没有办法在C中做到这一点?只要它们在gcc中运行,过度的预处理器杂技就可以了......

我所拥有的十六进制数据就是几千个字符串:
CRC(b746de6d)SHA1(ea69f87f84ded1f0a66457af24cbc692e5ff67e3)

真的,我只想要一个预处理器宏来执行一个简单的正则表达式,如
s /([a-fA-F0-9] [a-fA-F0-9])/ 0x \ 1,/这可能吗?
Hello, I have the following problem:

when declaring hex data in C, you typically do something like:
const char[] = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };
Better
const unsigned char[] = ....
0x** is an unsigned int value and the conversion to char can cause
an overflow if char is signed which leads to undefined behaviour.
This is quite verbose and makes the source hard to read.
Indeed.

An alternative is something like
const char[] = "deadbeef01020304";

but this doubles the storage requirements since each nybble is
converted to an ASCII char.
Nope. C does not specify anything about character encoding;
we can as well use EBCDIC or anything else. The 4bit nibbles
are represented by a character which has CHAR_BIT bits.

Some old assemblers have HEX directives for declaring large chunks of
raw data. My question is: is there any way to do this in C? Excessive
preprocessor acrobatics are OK as long as they work in gcc...

The hex data I have is several thousand strings like:
CRC(b746de6d) SHA1(ea69f87f84ded1f0a66457af24cbc692e5ff67e3)

Really, I just want a preprocessor macro that does a trivial regex like
s/([a-fA-F0-9][a-fA-F0-9])/0x\1,/ is that possible?




我担心你不会那样做;预处理器无法拆分预处理

令牌。


一些可能性:

如果您的目标系统上有文件I / O:在文本或二进制文件中存储数据块

,并在运行时检索它们。甚至可以使整个事情变得更加灵活。

查看通用预处理器或脚本语言

带有regexp并生成数据文件将转换为

所需的C数据,在适当的时候可以#include。

第三:如果你大胆,你可以伪造一个preprocesser宏;你打电话

,例如const unsigned char [] = HEXARRAY(deadbeef ....);

并在你经过

预处理和编译之前将perl脚本应用到你的源代码中;如果你使用

makefile,这是一个可行的解决方案。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



I fear you won''t get that; the preprocessor cannot split preprocessing
tokens.

Some possibilities:
If you have file I/O on your target system: Store your data chunks
in text or binary files and retrieve them at runtime. May even make
the whole thing more flexible.
Have a look at a general purpose preprocessor or a script language
with regexps and produce data files which will get converted to
the required C data which in turn can be #included where appropriate.
Third: If you are daring, you can fake a preprocesser macro; you "call"
e.g. const unsigned char[] = HEXARRAY(deadbeef....);
and apply, say, a perl script to your source before you go through
preprocessing and compilation; this is a viable solution if you use
makefiles.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


ar ****** @ mac.com 写道:
...
在C中声明十六进制数据时,通常会做类似的事情:
const char [] = {0xde,0xad,0xbe,0xef,0x01,0x02,0x03,0x04 ......};

这是非常冗长的,使得源难以阅读。

一个替代方案就像
const char [] =" deadbeef01020304";

但是这会使存储要求加倍,因为每个nybble都是
转换为ASCII字符。
...
when declaring hex data in C, you typically do something like:
const char[] = {0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04 ... };

This is quite verbose and makes the source hard to read.

An alternative is something like
const char[] = "deadbeef01020304";

but this doubles the storage requirements since each nybble is
converted to an ASCII char.




你也可以使用


const char [] =" \ xde \ xad \ xbe \ xef \ x01 \ x02 \ x03 \ x04";


虽然这并不比原版少得多。


-

祝你好运,

Andrey Tarasevich



You can also use

const char[] = "\xde\xad\xbe\xef\x01\x02\x03\x04";

although this is not much less verbose than the original version.

--
Best regards,
Andrey Tarasevich


Michael Mair写道:
Michael Mair wrote:
0x * *是一个unsigned int值,如果char被签名导致未定义的行为,转换为char可能导致溢出。
0x** is an unsigned int value and the conversion to char can cause
an overflow if char is signed which leads to undefined behaviour.




0x01是表达式类型为int,而不是unsigned。


将超出范围的值转换为有符号整数类型,

是实现定义的,不是未定义的。


-

pete



0x01 is an expression of type int, not unsigned.

Conversion of an out of range value, to a signed integer type,
is implementation defined, not undefined.

--
pete


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

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