文件大小端翻转工具
来源:互联网 发布:儿童编程教材 编辑:程序博客网 时间: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;
}
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
- 文件大小端翻转工具
- 文件大小工具类.
- 文件大小Byte工具转换类
- android文件大小输出工具类
- java 文件大小格式化显示工具
- java通用工具类 ---获取目录文件大小
- 文件大小
- 文件大小
- 翻转
- 翻转
- 翻转
- 你知道什么工具能压缩JavaScript脚本文件大小吗?
- Java获取文件大小,文件夹内文件个数的工具类
- Android中计算文件大小的工具类:FileSizeUtil
- 文件大小转换工具Formatter.formatFileSize(MainActivity.this, bitmap.getByteCount())
- JAVA不借助工具类翻转字符,数组
- 如何将网页所用图片裁剪成固定大小、合适文件大小(photoshop工具)
- android.text.format.Formatter计算文件大小超方便的工具类
- 二叉树层次遍历
- 我怎么才能改变在webstorm现有文件的文件类型关联?
- Maven教程,学习
- typedef 的用法及 具体使用步骤。
- 修改linux文件权限命令:chmod
- 文件大小端翻转工具
- 独立域名博客建立完成,欢迎来踩。
- log4j 集成sentry
- Andfix的apkpatch的工具使用
- Android轻量易用的键值数据库 QuickKV
- Java中数组的基础知识
- 获取一段文字中的某一串并标记成URL,SpannableStringBuilder
- iOS--APP 迭代开发版本号的规则
- javascript题库