文件大小端翻转工具

来源:互联网 发布:儿童编程教材 编辑:程序博客网 时间:2024/05/29 20:01
/*******************************************************************
include  file
*******************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
#include <iostream>


/*******************************************************************
code
*******************************************************************/
using namespace std;


typedef unsigned int u32;


#define ___constant_swab32(x) ((u32)( \
(((u32)(x) & (u32)0x000000ffUL) << 24) |\
(((u32)(x) & (u32)0x0000ff00UL) <<8) | \
(((u32)(x) & (u32)0x00ff0000UL) >>8) | \
(((u32)(x) & (u32)0xff000000UL) >> 24)))


/******************************************************************
 * 函数名: gos_get_cur_exe_path
 * 功能:获取 当前EXE的执行路径
 * 输入参数: 存放路径的指针,存放路径的长度
 * 输出参数:
 * 返回值: 成功与否
 * 作者: zhiwei
 * 更改:
 * 备注:
 *******************************************************************/
bool gos_get_cur_exe_path(char *path, int len)
{
int cnt = readlink("/proc/self/exe", path, len);
if(cnt < 0 || cnt >= len)
return false;


//delete the exe name
int i;
for(i = cnt; i >= 0; --i)
{
if(path[i] == '/')
{
path[i+1] = '\0';
break;
}
}
return true;
}
/******************************************************************
 * 函数名: getFileSize
 * 功能:获取 获取文件大小
 * 输入参数: 存放路径的指针,存放路径的长度
 * 输出参数:
 * 返回值: 成功与否
 * 作者: zhiwei
 * 备注:
 *******************************************************************/
int getFileSize(const char* path)   
{  
    FILE * fp = fopen(path, "r");  
    fseek(fp, 0L, SEEK_END);  
    int size = ftell(fp);  
    fclose(fp);  
    return size;  
}
/******************************************************************
   *  函数名: fs_write_from_sdram
   *  功能:从内存中读出数据
   *  输入参数:    file_path:要求写入的目标文件路径
    pSdram:要求写入的数据块首地址
    ulLen:要求写入的数据长度
   *  输出参数: 无
   *  返回值:   错误码
   *  作者: zhiwei
   *  更改:
   *  备注:
   *******************************************************************/
void fs_write_from_sdram(string file_path, char * pSdram, int ulLen)
{
int f_len = 0;
FILE *f_write;

/*向文件中写数据*/
if((f_write = fopen(file_path.c_str(), "wb")) == NULL)
{
cout<<"error ++++++"<<endl;
}

f_len = fwrite(pSdram, 1, ulLen, f_write);

fclose(f_write);


}
/******************************************************************
 * 函数名:main
 * 功能:字节序转换
 * 输入参数:
 * 输出参数:
 * 返回值:无
 * 作者:zhiwei.deng
 * 更改:
 * 备注:
 *******************************************************************/
int main()
{
int i;
char temp[100];
gos_get_cur_exe_path(temp, sizeof(temp));
string str_path = string(temp) + "src/src.bin";
string out_path = string(temp) + "out/out.bin";
creat(out_path.c_str(),0777);
int size = getFileSize(str_path.c_str());
int hfile = open(str_path.c_str(), 0, O_RDONLY);
char * p_buff = new char[size];
int len = read(hfile, p_buff, size);
cout<<"size = "<<size<<endl;

for (i = 0; i < size; i += 4) {
u32 *p = (u32 *)&p_buff[i];
*p = ___constant_swab32(*p);
}

fs_write_from_sdram(out_path,p_buff,size);

close(hfile);
return 1;
}
0 0
原创粉丝点击