1.将1个整型数据输入一个缓冲区char *buffer,实现跨平台传输,以二进制方式和文本方式分别实现写缓冲区和读缓冲区
---->在网络中传输是大端模式,而在本机上可能是小端模式,这个需要先判断
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char buf1[1024];
char buf2[1024];
int ascii_write_to(int);
int binary_write_to(int);
int binary_read_from();
int ascii_read_from();
int main(int argc, const char *argv[])
{
int a = 0x12345678;
binary_write_to(a);
ascii_write_to(a);
printf("%x %x\n", binary_read_from(), ascii_read_from());
return 0;
}
int ascii_write_to(int data)
{
sprintf(buf1, "%d", data);
return data;
}
int binary_write_to(int data)
{
short int x = 0x0001;
char *p = (char*)&x;
if(*p)//小端模式
{
int tmp = (data >> 24)&0xff | \
(data >> 8)&0xff00 | \
(data << 8)&0xff0000 |