Int to char [4] [英] Int to char[4]
问题描述
您好,我想知道如何将一个由四个元素组成的char数组
分配给int中使用的四个字节。截至目前我的代码是:
cNameSize =(char)((void)NameSize);
cFileSize =(char)((void)FileSize);
其中NameSize和FileSize是整数,cNameSize和
cFileSize是4个元素数组。这不行。
Hello, just wondering how I would assign a char array of four elements
to the four bytes used in an int. As of right now my code is:
cNameSize = (char)((void)NameSize);
cFileSize = (char)((void)FileSize);
Where NameSize and FileSize are the integers, and cNameSize and
cFileSize are 4 element arrays. This doesn''t work.
推荐答案
Th ************ @ gmail.com 写道:
您好,只是想知道如何将一个包含四个元素的char数组分配给int中使用的四个字节。截至目前我的代码是:
cNameSize =(char)((void)NameSize);
cFileSize =(char)((void)FileSize);
其中NameSize和FileSize是整数,cNameSize和
cFileSize是4个元素数组。这不起作用。
Hello, just wondering how I would assign a char array of four elements
to the four bytes used in an int. As of right now my code is:
cNameSize = (char)((void)NameSize);
cFileSize = (char)((void)FileSize);
Where NameSize and FileSize are the integers, and cNameSize and
cFileSize are 4 element arrays. This doesn''t work.
以上两者均无效。你的第一个问题是你不能完全分配数组。最简单的方法就是使用memcpy:
int NameSize;
char cNameSize [4];
memcpy(cNameSize,& NameSize,4);
Neither of the above are valid. Your first issue is you can''t
assign arrays at all. The easiest way is just to use memcpy:
int NameSize;
char cNameSize[4];
memcpy(cNameSize, &NameSize, 4);
在文章< 11 ******************* **@j27g2000cwj.googlegroups。 com>,
Th************@gmail.com 说...
In article <11*********************@j27g2000cwj.googlegroups. com>,
Th************@gmail.com says...
您好,我想知道如何将四个元素的char数组分配给四个字节
用于int。截至目前我的代码是:
cNameSize =(char)((void)NameSize);
cFileSize =(char)((void)FileSize);
其中NameSize和FileSize是整数,cNameSize和
cFileSize是4个元素数组。这不行。
Hello, just wondering how I would assign a char array of four elements
to the four bytes used in an int. As of right now my code is:
cNameSize = (char)((void)NameSize);
cFileSize = (char)((void)FileSize);
Where NameSize and FileSize are the integers, and cNameSize and
cFileSize are 4 element arrays. This doesn''t work.
你不可能做任何完全便携的事情。关于它的好处是:
union type_pun {
int i;
char c [sizeof(int)] ;
};
正式来说,它给出了未定义的行为,但是使用典型的编译器你可以将
写入一个int ,然后通过c访问它的各个字节。
-
后来,
杰里。
宇宙是自己想象的虚构。
You can''t do anything entirely portably. About as good as it gets is:
union type_pun {
int i;
char c[sizeof(int)];
};
Officially, it gives undefined behavior, but with a typical compiler you
can write an int into i, and then access its individual bytes via c.
--
Later,
Jerry.
The universe is a figment of its own imagination.
Jerry Coffin写道:
Jerry Coffin wrote:
在文章< 11 ********************* @ j27g2000cwj.googlegroups中。 com>,
Th************@gmail.com 说...
In article <11*********************@j27g2000cwj.googlegroups. com>,
Th************@gmail.com says...
>>您好,我想知道如何将四个元素的char数组分配给四个字节用于int。截至目前我的代码是:
cNameSize =(char)((void)NameSize);
cFileSize =(char)((void)FileSize);
其中NameSize和FileSize是整数,cNameSize和
cFileSize是4个元素数组。这不行。
>>Hello, just wondering how I would assign a char array of four elements
to the four bytes used in an int. As of right now my code is:
cNameSize = (char)((void)NameSize);
cFileSize = (char)((void)FileSize);
Where NameSize and FileSize are the integers, and cNameSize and
cFileSize are 4 element arrays. This doesn''t work.
你不可能做任何完全便携的事情。关于它的好处是:
union type_pun {
int i;
char c [sizeof(int)] ;
};
正式来说,它给出了未定义的行为,但是使用典型的编译器你可以将
写入一个int ,然后通过c访问它的各个字节。
You can''t do anything entirely portably. About as good as it gets is:
union type_pun {
int i;
char c[sizeof(int)];
};
Officially, it gives undefined behavior, but with a typical compiler you
can write an int into i, and then access its individual bytes via c.
假设你不关心字节顺序。
-
Ian Collins。
Assuming you aren''t concerned with the byte order.
--
Ian Collins.
这篇关于Int to char [4]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!