补充tools目录
This commit is contained in:
97
tools/bin_tool/bin2hex.c
Normal file
97
tools/bin_tool/bin2hex.c
Normal file
@@ -0,0 +1,97 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void print_binary(FILE *fp, uint32_t n)
|
||||
{
|
||||
uint8_t arr[32] = {0};
|
||||
int len = 0;
|
||||
for(len = 0; len < 32; len++){
|
||||
arr[len] = (n&1)+'0';
|
||||
n = n>>1;
|
||||
|
||||
}
|
||||
|
||||
len--;
|
||||
while (len >= 0 ){
|
||||
fprintf(fp,"%c", arr[len]);
|
||||
len--;
|
||||
}
|
||||
fprintf(fp,"\n");
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
uint8_t buf[128];
|
||||
FILE *fin = NULL, *fout = NULL;
|
||||
int i = 0;
|
||||
uint8_t val;
|
||||
off_t filesize, offset;
|
||||
size_t st = 0;
|
||||
uint32_t tmp = 0;
|
||||
|
||||
if( ( fin = fopen( argv[1], "rb" ) ) == NULL ){
|
||||
printf("fopen(%s,r) failed\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fout = fopen( argv[2], "w+" ) ) == NULL ){
|
||||
printf("fopen(%s,w+) failed\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( argc > 3){
|
||||
filesize = atoi(argv[3]);
|
||||
} else{
|
||||
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ){
|
||||
perror( "lseek" );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( fseek( fin, 0, SEEK_SET ) < 0 ){
|
||||
printf( "fseek(0,SEEK_SET) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
memset(buf, 0x0,128);
|
||||
for( offset = 0; offset < filesize; offset += 1 ){
|
||||
st = fread( buf, 1, 4, fin );
|
||||
if ( st == 0) {
|
||||
break;
|
||||
}
|
||||
#if 0
|
||||
for(i = 0; i < 4; i++){
|
||||
val = buf[3-i];
|
||||
printf("%02x\n", val);
|
||||
printf("%02x\n", val);
|
||||
fprintf(fout, "%02x\n", val);
|
||||
}
|
||||
#else
|
||||
if ( st == 4) {
|
||||
tmp = (uint32_t)(((uint32_t)buf[3] <<24)|((uint32_t)buf[2]<<16) |(uint32_t)(buf[1]<<8)|(uint32_t)buf[0]);
|
||||
} else if (st == 3) {
|
||||
tmp = (uint32_t)(((uint32_t)buf[2]<<16) |(uint32_t)(buf[1]<<8)|(uint32_t)buf[0]);
|
||||
} else if (st == 2) {
|
||||
tmp = (uint32_t)((uint32_t)(buf[1]<<8)|(uint32_t)buf[0]);
|
||||
} else if (st == 1) {
|
||||
tmp = (uint32_t)((uint32_t)buf[0]);
|
||||
}
|
||||
//printf("%02x %02x %02x %02x\n", buf[3], buf[2], buf[1], buf[0]);
|
||||
//printf("%08x\n", tmp);
|
||||
fprintf(fout, "%08x\n", tmp);
|
||||
#endif
|
||||
}
|
||||
|
||||
exit:
|
||||
if(fin){
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
if(fout){
|
||||
fclose(fout);
|
||||
}
|
||||
}
|
116
tools/bin_tool/bin2hex_frame.c
Normal file
116
tools/bin_tool/bin2hex_frame.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define DATA_NUM_PER_ROW 16
|
||||
|
||||
void print_binary(FILE *fp, uint32_t n)
|
||||
{
|
||||
uint8_t arr[32] = {0};
|
||||
int len = 0;
|
||||
for(len = 0; len < 32; len++){
|
||||
arr[len] = (n&1)+'0';
|
||||
n = n>>1;
|
||||
|
||||
}
|
||||
|
||||
len--;
|
||||
while (len >= 0 ){
|
||||
fprintf(fp,"%c", arr[len]);
|
||||
len--;
|
||||
}
|
||||
fprintf(fp,"\n");
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
char buf[128];
|
||||
FILE *fin = NULL, *fout = NULL;
|
||||
int i = 0;
|
||||
uint8_t val;
|
||||
off_t filesize, offset;
|
||||
uint32_t data_counter = 0;
|
||||
uint8_t fill_zero_num = 0;
|
||||
|
||||
printf("argc:%d, ", argc);
|
||||
for (uint8_t i = 0; i < argc; i++) {
|
||||
printf("argv[%d]:%s, ", i, argv[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
if( ( fin = fopen( argv[1], "rb+" ) ) == NULL ){
|
||||
printf("fopen(%s,r) failed\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fout = fopen( argv[2], "w+" ) ) == NULL ){
|
||||
printf("fopen(%s,w+) failed\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( argc > 3){
|
||||
filesize = atoi(argv[3]);
|
||||
} else{
|
||||
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ){
|
||||
perror( "lseek" );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
// //4-byte alignment, fill in zeros if not enough
|
||||
// fill_zero_num = filesize % 4;
|
||||
// if (fill_zero_num != 0) {
|
||||
// if (fseek(fin, 0, SEEK_END) < 0) {
|
||||
// printf( "fseek(0,SEEK_END) failed\n" );
|
||||
// goto exit;
|
||||
// }
|
||||
// char temp = 0;
|
||||
// if (fwrite(&temp, 1, fill_zero_num, fin) != fill_zero_num) {
|
||||
// printf("fwrite(0,%d) failed\n", fill_zero_num);
|
||||
// goto exit;
|
||||
// }
|
||||
// filesize++;
|
||||
// }
|
||||
|
||||
if( fseek( fin, 0, SEEK_SET ) < 0 ){
|
||||
printf( "fseek(0,SEEK_SET) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
memset(buf, 0x0,128);
|
||||
for( offset = 0; offset < filesize; offset += 1 ){
|
||||
if( fread( buf, 1, 4, fin ) != (size_t) 4 ){
|
||||
goto exit;
|
||||
}
|
||||
for(i = 0; i < 4; i++){
|
||||
// val = buf[3-i];
|
||||
val = buf[i];
|
||||
printf("0x%02x", val);
|
||||
fprintf(fout, "0x%02x", val);
|
||||
data_counter++;
|
||||
if (data_counter == filesize) {
|
||||
break;
|
||||
}
|
||||
if (data_counter % DATA_NUM_PER_ROW != 0) {
|
||||
printf(", ");
|
||||
fprintf(fout, ", ");
|
||||
} else {
|
||||
printf(",\n");
|
||||
fprintf(fout, ",\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
if(fin){
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
if(fout){
|
||||
fclose(fout);
|
||||
}
|
||||
return 0;
|
||||
}
|
117
tools/bin_tool/hex2bin.c
Normal file
117
tools/bin_tool/hex2bin.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define strtoul(cp, endp, base) simple_strtoul(cp, endp, base)
|
||||
|
||||
void print_binary(FILE *fp, uint32_t n)
|
||||
{
|
||||
uint8_t arr[32] = {0};
|
||||
int len = 0;
|
||||
for(len = 0; len < 32; len++){
|
||||
arr[len] = (n&1)+'0';
|
||||
n = n>>1;
|
||||
|
||||
}
|
||||
|
||||
len--;
|
||||
while (len >= 0 ){
|
||||
fprintf(fp,"%c", arr[len]);
|
||||
len--;
|
||||
}
|
||||
fprintf(fp,"\n");
|
||||
}
|
||||
|
||||
unsigned long simple_strtoul(const char *cp, char **endp,
|
||||
unsigned int base)
|
||||
{
|
||||
unsigned long result = 0;
|
||||
unsigned long value;
|
||||
|
||||
if (*cp == '0') {
|
||||
cp++;
|
||||
if ((*cp == 'x') && isxdigit(cp[1])) {
|
||||
base = 16;
|
||||
cp++;
|
||||
}
|
||||
|
||||
if (!base)
|
||||
base = 8;
|
||||
}
|
||||
|
||||
if (!base)
|
||||
base = 10;
|
||||
|
||||
while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
|
||||
? toupper(*cp) : *cp)-'A'+10) < base) {
|
||||
result = result*base + value;
|
||||
cp++;
|
||||
}
|
||||
|
||||
if (endp)
|
||||
*endp = (char *)cp;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
char buf[128];
|
||||
FILE *fin = NULL, *fout = NULL;
|
||||
int i = 0;
|
||||
uint8_t val;
|
||||
off_t filesize, offset;
|
||||
uint32_t tmp = 0;
|
||||
|
||||
if( ( fin = fopen( argv[1], "rb" ) ) == NULL ){
|
||||
printf("fopen(%s,r) failed\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fout = fopen( argv[2], "w+" ) ) == NULL ){
|
||||
printf("fopen(%s,w+) failed\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( argc > 3){
|
||||
filesize = atoi(argv[3]);
|
||||
} else{
|
||||
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ){
|
||||
perror( "lseek" );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( fseek( fin, 0, SEEK_SET ) < 0 ){
|
||||
printf( "fseek(0,SEEK_SET) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
memset(buf, 0x0, 128);
|
||||
char *st = fgets(buf,128, fin);
|
||||
char **endp = NULL;
|
||||
|
||||
if (st == NULL) {
|
||||
break;
|
||||
}
|
||||
tmp = strtoul(buf, endp, 16);
|
||||
//printf("%s:%08x\n", buf, tmp);
|
||||
//fprintf(fout, "%08x\n", tmp);
|
||||
print_binary(fout, tmp);
|
||||
}
|
||||
|
||||
exit:
|
||||
if(fin){
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
if(fout){
|
||||
fclose(fout);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user