我怎样才能重新排序在C整数的字节? [英] How can I reorder the bytes of an integer in c?
问题描述
我的任务是数据文件从一个端转换到另一个(大尾数为小端和放大器;反之亦然)使用C.我一直在网上找了3个小时左右,现在其他的例子,读我的文字书,但是我这么卡在如何甚至开始这个功能。到目前为止,我有正确的事件(1〜4)订单,但里面我的convert_and_save功能,我必须用创建一个字符数组 - >字符缓冲区[4];是否有人可以帮助我吗?即使你只是给我什么来查找线索,我将不胜AP preciate它。
我需要写一个调用的函数:
无效convert_and_save(结构记录项目,FILE * output_handle,INT数);
此函数内我做下面的一系列步骤:
(1)转换成整数使用字符数组:
INT integer_to_characters(INT号,字符*缓冲)
{
的memcpy(缓冲液,及放大器;数,4);
}
(2)反向该数组中字符的顺序。
(3)使用转换的字符阵列回的整数:
INT characters_to_integer(字符*缓冲区)
{
INT结果;
的memcpy(&放大器;结果,缓冲,4);
返回结果;
}
(4)用写转换记录的输出文件:
无效save_record(FILE * file_handle,结构记录的)
{
烧焦output_buffer [size_of_record]
integer_to_characters(a.age,及(output_buffer [0]));
的memcpy(及(output_buffer [4]),a.name,12);
integer_to_characters(a.department,及(output_buffer [16]));
FWRITE(output_buffer,sizeof的(炭),size_of_record,file_handle);
}
我写的鹦鹉VM的功能这一项,你可以从鹦鹉code.org byteorder.c。有可能更短的方式来做到这一点,但是这适用于不同大小的整数,我们有一个宏观检测PARROT_BIGENDIAN平台的byteorder,则可以放弃一切。此外,如上面提到的,你可以搜索出htonl(),这是在大尾端硬件NOP,但在转换成小端(只抓一个Linux x86的实现)
INTVAL
fetch_iv_le(INTVAL W)
{
ASSERT_ARGS(fetch_iv_le)
#如果!PARROT_BIGENDIAN
返回瓦; //在小端硬件空操作
#其他
#如果INTVAL_SIZE == 4
返回(W<< 24)| ((W&安培;为0xFF00)LT;< 8)| ((W&放大器;为0xFF0000)GT;→8)| (并且R w> 24);
其他#
#如果INTVAL_SIZE == 8
INTVAL - [R; R = W&所述;&下; 56;
- [R | =(W&安培;为0xFF00)LT;< 40;
- [R | =(W&安培;为0xFF0000)LT;< 24;
- [R | =(W&安培; 0xff000000)LT;< 8;
- [R | =(W&安培; 0xff00000000)GT;> 8;
- [R | =(W&安培; 0xff0000000000)GT;> 24;
- [R | =(W&安培; 0xff000000000000)GT;> 40;
- [R | =(W&安培; 0xff00000000000000)GT;> 56;
返回ř;
# 万一
# 万一
#万一
}
My task is to convert a data file from one endian to another (big endian to little endian & vice versa) using C. I have been looking online for about 3 hours now for other examples and reading my text book, however I am so stuck on how to even start this function. So far I have the order of events correct(1 through 4) but inside my "convert_and_save" function do I have to create a char array using--> char buffer[4]; Can someone please help me? even if you just give me clues on what to look up, I would greatly appreciate it.
I need to write a function called:
void convert_and_save(struct record item, FILE * output_handle, int number);
inside this function I do the following series of steps:
(1) Convert the integer into an array of chars using:
int integer_to_characters(int number, char * buffer)
{
memcpy(buffer, &number, 4);
}
(2) Reverse the order of the chars in that array.
(3) Convert the array of chars back to an integer using:
int characters_to_integer(char * buffer)
{
int result;
memcpy(&result, buffer, 4);
return result;
}
(4) write the converted record on the output file using:
void save_record(FILE * file_handle, struct record a)
{
char output_buffer[size_of_record];
integer_to_characters(a.age, &(output_buffer[0]));
memcpy(&(output_buffer[4]), a.name, 12);
integer_to_characters(a.department, &(output_buffer[16]));
fwrite(output_buffer, sizeof(char), size_of_record, file_handle);
}
This one of the functions I wrote for the Parrot VM, you can download byteorder.c from parrotcode.org. There are probably shorter ways to do it, but this works for different size integers, and we had a macro to detect the byteorder of the platform in PARROT_BIGENDIAN, you can discard all that. Also, as mentioned above, you can search out htonl() which is a nop on bigendian hardware, but converts on little endian (just grab a Linux x86 implementation)
INTVAL
fetch_iv_le(INTVAL w)
{
ASSERT_ARGS(fetch_iv_le)
#if !PARROT_BIGENDIAN
return w; // No-op on little endian hardware
#else
# if INTVAL_SIZE == 4
return (w << 24) | ((w & 0xff00) << 8) | ((w & 0xff0000) >> 8) | (w >> 24);
# else
# if INTVAL_SIZE == 8
INTVAL r;
r = w << 56;
r |= (w & 0xff00) << 40;
r |= (w & 0xff0000) << 24;
r |= (w & 0xff000000) << 8;
r |= (w & 0xff00000000) >> 8;
r |= (w & 0xff0000000000) >> 24;
r |= (w & 0xff000000000000) >> 40;
r |= (w & 0xff00000000000000) >> 56;
return r;
# endif
# endif
#endif
}
这篇关于我怎样才能重新排序在C整数的字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!