初學Python寫二進制文件python
把一個圖片的16進制數據保存到一個txt文本,從這個txt文本讀出並保存爲二進制文件jpg圖片文件。說明:圖片讀出的0xff粘貼ff到文本中,讀出時是字符串的」ff」。數組
我主要是用C語言,python爲初學,python的編碼思想仍是用C的思想。函數
1、C的實現:測試
#include <stdio.h>編碼
#include <string.h>code
/*******************************圖片
函數名:DSP_2_HEXutf-8
函數功能:字符串與十六進制數據轉換字符串
函數輸入參數:dsp 要轉換的字符串的地址get
hex 轉換16進制後的存放地址
count 轉換16進制後的長度
"1234"->{0x12,0x34}
修改: 較嚴格判斷字符類型 ‘0’~’9’ ’A’~’F’ ‘a’~’f’
********************************/
int DSP_2_HEX( unsigned char *dsp, unsigned char *hex, int count )
{
int i;
unsigned char ucTmp = 0;
for (i = 0; i < count; i++)
{
ucTmp = dsp[i*2];
/*將字符轉爲十六進制 ‘0’~’9’->0x00~0x0九、 ’A’~’F’->0x0A~0x0F ‘a’~’f’->0x0A~0x0F*/
if(ucTmp>=0x30 && (ucTmp<=0x39))
{
hex[i] = ucTmp-0x30;
}
else if(ucTmp >= 0x41 && ucTmp<= 0x46)
{
hex[i] = ucTmp-0x41+10 ;
}
else if (ucTmp >= 0x61 && ucTmp<= 0x66)
{
hex[i] = ucTmp-0x61+10 ;
}
else
{
return -1;
}
/*第一個字符做爲十六進制的高4位*/
hex[i] = hex[i] << 4;
ucTmp = dsp[i*2+1];
/*將字符轉爲十六進制 ‘0’~’9’->0x00~0x0九、 ’A’~’F’->0x0A~0x0F ‘a’~’f’->0x0A~0x0F*/
if(ucTmp>=0x30 && (ucTmp<=0x39))
{
hex[i] += ucTmp-0x30;
}
else if(ucTmp >= 0x41 && ucTmp<= 0x46)
{
hex[i] += ucTmp-0x41+10 ;
}
else if (ucTmp >= 0x61 && ucTmp<= 0x66)
{
hex[i] += ucTmp-0x61+10 ;
}
else
{
return -1;
}
/*// 只有大寫的字母能夠轉到十六進制
hex[i] = ( (dsp[i*2]<=0x39)? dsp[i*2]-0x30 : dsp[i*2]-0x41+10 );
hex[i] = (hex[i] << 4) &0xF0;
hex[i]+= ( (dsp[i*2+1]<=0x39) ? dsp[i*2+1]-0x30 : dsp[i*2+1]-0x41+10 );
*/
}
return 0;
}
int main()
{
FILE *fp;
int i = 0;
int j =0;
int len = 0;
unsigned char buffer[40*1024] = {0}; //從txt文本里讀出的字符串
unsigned char photo[40*1024] = {0}; //將字符串中空格、回車等去掉後字符串
unsigned char photoHEX[20*1024] = {0}; //轉爲十六進制後的數組
int ch;
/*從txt中讀出字符保存到buffer數組*/
fp = fopen("D:\\photo.txt","r");
if(fp != NULL)
{
while((ch=fgetc(fp))!=EOF)
{
buffer[i] = ch;
i++;
}
}
fclose(fp);
fp = NULL;
len = i; //字符長度
/*過濾掉字符串中的空格、回車(換行)、製表符*/
for (i = 0; i<len; i++)
{
if(buffer[i]!=' ' && buffer[i]!= '\r' && buffer[i]!='\n' && buffer[i]!='\t'))
{
photo[j] = buffer[i];
j++;
}
}
/*將過濾後的字符串轉爲十六進制*/
len = j/2; //要轉換爲十六進制的長度
memset(photoHEX,0,sizeof(photoHEX));
DSP_2_HEX(photo,photoHEX,len);
/*保存爲二進制圖片*/
fp = fopen("D:\\photo.jpg","wb");
if(fp != NULL)
{
fwrite(photoHEX,1,len,fp);
}
fclose(fp);
fp = NULL;
return 1;
}
2、python實現
Python直接以腳本方式,沒有寫成模塊的方式。
# -*- coding: utf-8 -*-
#codeing=utf-8
import struct
"""
打開txt文件,並讀出文件內容,保存到str類型的filet變量中
"""
with open("D:\\photo.txt","r") as f:
filet= f.read();
print(type(filet)) #查看filet類型打印爲<type 'str'>
strY = '' #創建空字符串保存過濾後的字符串
i = 0
"""
打開二進制文件,並把txt的字符串轉換爲二進制後寫入
"""
with open("D:\\photo.jpg","wb") as g:
for x in filet : #逐個字符迭代
if x != ' ' and x!= '\n' and x!= '\r' and x!= '\t': #過濾掉空格、回車(換行)、製表符
strY += x #將字符串鏈接到strY
i+=1;
if(i%2 == 0): #每兩個字符轉換一次爲HEX,並寫入文件
xHex = int(strY,16) #將兩個字符轉爲HEX 「ff 」->0x000000ff
xHex = struct.pack("B",xHex) #將HEX數據轉爲unsigned char的字符串
g.write(xHex) #write()函數參數只能爲str類型
strY = '' #strY 置爲空字符串
Struct模塊的pack和unpack不在這裏介紹。
有這個測試的緣由是由於,在嵌入式中讀出串口攝像頭返回的數據並在仿真模式下在控制檯打印了出來。目的把讀出來的數據寫成圖片文件,驗證攝像頭返回的數據是否正確。雖然本嵌入式中沒有文件系統,但能夠直接在嵌入式中的仿真模式下直接寫成圖片文件就能夠驗證,
/*保存爲二進制圖片*/
fp = fopen("D:\\photo.jpg","wb");
if(fp != NULL)
{
fwrite(photoHEX,1,len,fp);
}
fclose(fp);
fp = NULL;
結果兜了一大圈來實現。吐槽一下思惟僵化。