从字节串中可移植地提取数据 [英] Portably extracting data from a bytestring

查看:67
本文介绍了从字节串中可移植地提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设S是指向长度为L的字节串的指针。我想在位置p = S + d处从S提取4

字节,其中0 << d< L - 4,将它们存入

到unsigned int中。我正在寻找关于如何做到这一点的建议


1)便携式ANSI C.

2)尽可能高效。

3)充分考虑潜在的数据调整和这个行动必须解决的
字节序问题。


Let S be a pointer to a bytestring of length L. I would like to extract 4
bytes from S at the location p = S + d, with 0 < d < L - 4, and store them
into an unsigned int. I am looking for suggestions on how to do this

1) In portable ANSI C.
2) As efficiently as possible.
3) Taking full account of the potential data alignment and
endianness issues that this action must tackle.


推荐答案

James S. Singleton写道:
James S. Singleton wrote:
设S是指向长度为L的字节串的指针。我想从该位置的S中提取4个字节p = S + d,0 < d< L - 4,并将它们存储到unsigned int中。我正在寻找关于如何做到这一点的建议

1)便携式ANSI C.
2)尽可能高效。
3)充分考虑潜在的数据对齐和这个动作必须解决的字节序问题。
Let S be a pointer to a bytestring of length L. I would like to extract 4
bytes from S at the location p = S + d, with 0 < d < L - 4, and store them
into an unsigned int. I am looking for suggestions on how to do this

1) In portable ANSI C.
2) As efficiently as possible.
3) Taking full account of the potential data alignment and
endianness issues that this action must tackle.



typedef union {

unsigned char c [sizeof(unsigned int)] ;

unsigned int i;

} U;


unsigned int convert(char * S,int d)

{

你好;

memcpy(& u,S + d,sizeof(unsigned int));

返回ui;

}


这假设在给定位置存储了一个整数。

问题是你做了没有定义什么提取四个字节

和将它们存储在unsigned int中。真的意思是。


如果你不关心对齐(x86架构)你可以


unsigned int convert(char * S,int d )

{

U * u;

u =(U *)(S + d);

返回u-> i;

}

效率更高,但你可以得到一个对齐陷阱。


两者都认为

1)你已经存储在该位置的整数之前

2)你在相同的机器架构中读取它们。


jacob


typedef union {
unsigned char c[sizeof(unsigned int)];
unsigned int i;
} U;

unsigned int convert(char *S,int d)
{
U u;
memcpy(&u,S+d,sizeof(unsigned int));
return u.i;
}

This assumes that at the given location an integer was stored.
The problem is that you did not define what "extract four bytes"
and "store them in an unsigned int" really means.

If you do not care about alignment (x86 architecture) you could

unsigned int convert(char *S,int d)
{
U *u;
u = (U *)(S+d);
return u->i;
}
More efficient, but you could get an alignment trap.

Both suppose that
1) You have stored before an integer at that location
2) You read them in the same machine architecture.

jacob


jacob navia< ja *** @ jacob.remcomp.fr>写道:
jacob navia <ja***@jacob.remcomp.fr> writes:
James S. Singleton写道:
James S. Singleton wrote:
让S成为长度为L的字节串的指针。我想提取4
在位置p = S + d处来自S的字节,其中0 <1。 d< L - 4,并将它们存储到unsigned int中。我正在寻找关于如何做到这一点的建议
1)在便携式ANSI C中.2)尽可能高效。
3)充分考虑潜在的数据一致性和
字节序问题这个动作必须解决。
Let S be a pointer to a bytestring of length L. I would like to extract 4
bytes from S at the location p = S + d, with 0 < d < L - 4, and store them
into an unsigned int. I am looking for suggestions on how to do this
1) In portable ANSI C. 2) As efficiently as possible.
3) Taking full account of the potential data alignment and
endianness issues that this action must tackle.


typedef union {
unsigned char c [sizeof(unsigned int)];
unsigned int i;
} U;

unsigned int convert(char * S,int d)
{
u u;
memcpy(& u,S + d,sizeof(unsigned int));
return ui;
}


typedef union {
unsigned char c[sizeof(unsigned int)];
unsigned int i;
} U;

unsigned int convert(char *S,int d)
{
U u;
memcpy(&u,S+d,sizeof(unsigned int));
return u.i;
}




为什么不呢:


unsigned int convert(char * s ,int d)

{

unsigned int i;

memcpy(& i,s + d,sizeof i);

返回i;

}


允许任何类型的字符访问; memcpy()进行角色访问。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q ){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

) ;}返回0;}



Why not just this:

unsigned int convert(char *s, int d)
{
unsigned int i;
memcpy(&i, s + d, sizeof i);
return i;
}

Character access is allowed to any type; memcpy() does character access.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


文章< 43 ********************** @ news .wanadoo.fr>,

jacob navia< ja *** @ jacob.remcomp.fr>写道:
In article <43**********************@news.wanadoo.fr>,
jacob navia <ja***@jacob.remcomp.fr> wrote:
James S. Singleton写道:
James S. Singleton wrote:
设S是指向长度为L的字节串的指针。我想从S中提取4个字节位置p = S + d,0 <0。 d< L - 4,并将它们存储到unsigned int中。我正在寻找有关如何执行此操作的建议
1)在便携式ANSI C中。
Let S be a pointer to a bytestring of length L. I would like to extract 4
bytes from S at the location p = S + d, with 0 < d < L - 4, and store them
into an unsigned int. I am looking for suggestions on how to do this 1) In portable ANSI C.


typedef union {
unsigned char c [sizeof(unsigned int)];
unsigned int i;
} U;
unsigned int convert(char * S,int d)
{
u u;
memcpy(& u,S + d,sizeof(unsigned int));
return ui;
}

typedef union {
unsigned char c[sizeof(unsigned int)];
unsigned int i;
} U; unsigned int convert(char *S,int d)
{
U u;
memcpy(&u,S+d,sizeof(unsigned int));
return u.i;
}




目前我找不到条款,但我相对肯定

行为是未定义的,以便从

联盟中读取一个联盟成员,除非它与最后一次写的相同[除了你要从中检索的情况

相同的基本类型

在具有共同前缀的工会成员中。]


您可以更安全地将对象指针强制转换为char *。

-

当时我很年轻,但我也很沮丧。

- 克里斯托弗牧师



I can''t find the clause at the moment, but I''m relatively sure
that the behaviour is undefined to read a union member out of a
union unless it was the same one last written [except for cases
where you are retrieving from the same fundamental types
in union members with common prefixes.]

You are on safer grounds to cast the object pointer to char* .
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest


这篇关于从字节串中可移植地提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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