特定类型的十六进制转换 [英] specific type of Hex conversion

查看:90
本文介绍了特定类型的十六进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


i找到了一个proggie,将捕获的数据包(通过libpcap)转换为hex

格式...将它们传递给dnet-申请。


现在,proggie显示结果如下:

4500003ca92e00002001c96a0a0a0a0b0a0a0a0a08007ca401 00cfb76162636465666768696a6b6c6d6e6f70717273747576 77616263646566676869

(请转到底部代码)


虽然我需要的结果如下:


\ x45 \ x00 \ x00 \ x3c \\ \\ xa9 \x2e \\ xxx \ x00 \ x20 \ x01 \ xc9 \ x6a \ x 0a \ x0 \\\ x0a \ x0b \ x0 \ \\ x0a \ x0a \ x0a \ x08\x00\x7c\xa4 \x01 \x00 \ xcf \ xb7 \ x61 \ x62 \ x63 \ x64 \ x65 \ x66 \ x67 \ x68 \ x69 \\ x 6a \x6b \ x6c \\ x6d \ x6e \\ xxf \ x70 \ x71 \ x72 \ x73 \ x74 \ x75 \ x76 \ x77 \ x61 \ x62 \ x63\x64\x65\x66 \\ x67 \ x68 \ x69


我试图操纵这些值,但它仍然显示数字为

块4(例如\\ \\ x4500)而不是2块(例如

\ x45 \ x00)。


这是proggie的代码,也许是某人会帮我把它对齐

我想要的吗?


///////////////
< br $>
void

print_hex(u_char * packet,u_short len)

{

int i,s_cnt;

u_short * p;


p =(u_short *)包;

s_cnt = len / sizeof(u_short);


for(i = 0; --s_cnt> = 0; i ++)

{

if((!(i%60)))

{

if(i != 0)

{

printf(" \ n");

}


}

printf("%04x",ntohs(*(p ++)));


}


if(len& 1)

{

if((!(i%8)))

{

// printf(" \ n%02x \t",(i * 2));

}

printf("%) 02x",*(u_char *)p);

}

printf(" \ n");

}

Hi guys,

i found a proggie, to convert packets captured (by libpcap) to hex
format...to pass them to dnet-application.

Now the proggie shows results as following:
4500003ca92e00002001c96a0a0a0a0b0a0a0a0a08007ca401 00cfb76162636465666768696a6b6c6d6e6f70717273747576 77616263646566676869

(please go to bottom for code)

While i need the result to look as follows:

\x45\x00\x00\x3c\xa9\x2e\x00\x00\x20\x01\xc9\x6a\x 0a\x0a\x0a\x0b\x0a\x0a\x0a\x0a\x08\x00\x7c\xa4\x01 \x00\xcf\xb7\x61\x62\x63\x64\x65\x66\x67\x68\x69\x 6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76 \x77\x61\x62\x63\x64\x65\x66\x67\x68\x69

I tried to manipulate the values, but it still showed the numbers as a
block of 4 (for example \x4500) instead of a block of 2 (for example
\x45\x00).

Here is the code of the proggie, maybe someone would help me align it
as i want to?

///////////////

void
print_hex(u_char *packet, u_short len)
{
int i, s_cnt;
u_short *p;

p = (u_short *)packet;
s_cnt = len / sizeof(u_short);

for (i = 0; --s_cnt >= 0; i++)
{
if ((!(i % 60)))
{
if (i != 0)
{
printf("\n");
}

}
printf("%04x", ntohs(*(p++)));

}

if (len & 1)
{
if ((!(i % 8)))
{
//printf("\n%02x\t", (i * 2));
}
printf("%02x ", *(u_char *)p);
}
printf("\n");
}

推荐答案

开始Kifah Abbad:
begin Kifah Abbad:
4500003ca92e00002001c96a0a0a0a0b0a0a0a0a08007 [...]

虽然我需要结果看起来如下:

\ x45 \ x00 \ x00 \ x3c \\ xx9 \ x2e \ x00 \ x00 \ x20 \ x01 \ xc9 [... ]


转换:

printf("%04x",ntohs(*(p ++)));


to


{short tmp = ntohs(*(p ++));

printf(" \\ \\\x%02x \\ x%02x",tmp>> 8,tmp& 0xFF); }


和这个

printf("%02x",*(u_char *)p);




printf(" \\x%02x",*(u_char *)p);
4500003ca92e00002001c96a0a0a0a0b0a0a0a0a08007 [...]

While i need the result to look as follows:

\x45\x00\x00\x3c\xa9\x2e\x00\x00\x20\x01\xc9 [...]
Transform this:
printf("%04x", ntohs(*(p++)));
to

{ short tmp = ntohs(*(p++));
printf("\\x%02x\\x%02x", tmp >> 8, tmp & 0xFF); }

and this
printf("%02x ", *(u_char *)p);
to
printf("\\x%02x ", *(u_char *)p);




-
$ b $bFürGoogle,Tux和GPL!



--
Für Google, Tux und GPL!


" Alexander Bartolich" <人***************** @ gmx.at>在留言中写道

news:bs ************ @ ID-193444.news.uni-berlin.de ...

(没什么,根据我的新闻阅读器)


你能否删除开始Kifah Abbad:来自

的消息开头,因为它非常令人困惑

我的新闻阅读器,创建一个名为Kifah Abbad_的附件

是不可读的(可能是错误的解码)。


我可以通过命令查看你的消息来查看原始的

消息源(Ctrl-F3),但是这不太理想。


-

Simon。
"Alexander Bartolich" <al*****************@gmx.at> wrote in message
news:bs************@ID-193444.news.uni-berlin.de...
(nothing, according to my newsreader)

Can you please remove the "begin Kifah Abbad:" line from
the beginning of your messages, as it is highly confusing
my newsreader, creating an attachment called "Kifah Abbad_"
which is unreadable (probably been wrongly decoded).

I can read your messages with the command to view the raw
message source (Ctrl-F3), but that is not ideal.

--
Simon.


2003年12月20日14: 43:04 -0800, ki***@web.de (Kifah Abbad)写道:
On 20 Dec 2003 14:43:04 -0800, ki***@web.de (Kifah Abbad) wrote:
大家好,

我发现了一个程序,将捕获的数据包(通过libpcap)转换为hex
格式...将它们传递给dnet-application。

现在,proggie显示结果如下:
4500003ca92e00002001c96a0a0a0a0b0a0a0a0a08007ca40 100cfb76162636465666768696a6b6c6d6e6f7071727374757 677616263646566676869





虽然我需要将结果视为如下:

\\ x45 \ x00 \x00 \ x3c \ xa9 \ x2e \\ xx \ x00 \ x20 \ x01 \ xc9 \ x6a \ x0a \ x0a \ x0a \ x0b \ x0a \ x0x \ x0 \\ t \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\ x66 \ x67 \ x68 \ x69 \ x6a \ x6b \ x6c \\ x6d \ x6e \\ xxf \ x70 \ x71 \ x72 \ x73 \ x74 \ x75 \ x7 6 \ x77 \ x61 \ x62 \ x63 \ x64 \ x65 \ x66 \ x67 \ x68 \ x69

我试图操纵这些值,但它仍然显示数字为4块(例如\ x4500)而不是2块(例如
\ x45 \ x00)。

这是proggie的代码,也许有人会帮我按照我的意愿调整它

///////////////

void
print_hex(u_char * packet,u_short len)
{
int i,s_cnt;
u_short * p;

p =(u_s hort *)包;


你怎么知道数据包中的值被正确对齐为u_short的

地址(我假设它是一个无符号的短整数)。

s_cnt = len / sizeof(u_short);

for(i = 0; --s_cnt> = 0; i ++)
{
if((!(i%60)))
{
if(i!= 0)
{
printf(" \ n");
}


printf("%04x",ntohs(*(p ++)));

}

if( len& 1)
{
if((!(i%8)))
{printf(" \ n%02x \t", (i * 2));
}
printf("%02x",*(u_char *)p);
}
printf(" \ n" );
}
Hi guys,

i found a proggie, to convert packets captured (by libpcap) to hex
format...to pass them to dnet-application.

Now the proggie shows results as following:
4500003ca92e00002001c96a0a0a0a0b0a0a0a0a08007ca40 100cfb76162636465666768696a6b6c6d6e6f7071727374757 677616263646566676869

(please go to bottom for code)

While i need the result to look as follows:

\x45\x00\x00\x3c\xa9\x2e\x00\x00\x20\x01\xc9\x6a\ x0a\x0a\x0a\x0b\x0a\x0a\x0a\x0a\x08\x00\x7c\xa4\x0 1\x00\xcf\xb7\x61\x62\x63\x64\x65\x66\x67\x68\x69\ x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x7 6\x77\x61\x62\x63\x64\x65\x66\x67\x68\x69

I tried to manipulate the values, but it still showed the numbers as a
block of 4 (for example \x4500) instead of a block of 2 (for example
\x45\x00).

Here is the code of the proggie, maybe someone would help me align it
as i want to?

///////////////

void
print_hex(u_char *packet, u_short len)
{
int i, s_cnt;
u_short *p;

p = (u_short *)packet;
How do you know that the value in packet is properly aligned to be the
address of a u_short (which I assume is an unsigned short int).
s_cnt = len / sizeof(u_short);

for (i = 0; --s_cnt >= 0; i++)
{
if ((!(i % 60)))
{
if (i != 0)
{
printf("\n");
}

}
printf("%04x", ntohs(*(p++)));

}

if (len & 1)
{
if ((!(i % 8)))
{
//printf("\n%02x\t", (i * 2));
}
printf("%02x ", *(u_char *)p);
}
printf("\n");
}




<<删除电子邮件的del>>



<<Remove the del for email>>


这篇关于特定类型的十六进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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