python写二进制文件_初学Python写二进制文件
初學Python寫二進制文件
把一個圖片的16進制數(shù)據(jù)保存到一個txt文本,從這個txt文本讀出并保存為二進制文件jpg圖片文件。說明:圖片讀出的0xff粘貼ff到文本中,讀出時是字符串的”ff”。
我主要是用C語言,python為初學,python的編碼思想還是用C的思想。
一、C的實現(xiàn):
#include
#include
/*******************************
函數(shù)名:DSP_2_HEX
函數(shù)功能:字符串與十六進制數(shù)據(jù)轉換
函數(shù)輸入?yún)?shù):dsp 要轉換的字符串的地址
hex 轉換16進制后的存放地址
count 轉換16進制后的長度
"1234"->{0x12,0x34}
修改: 較嚴格判斷字符類型 ‘0’~’9’ ’A’~’F’ ‘a(chǎn)’~’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~0x09、 ’A’~’F’->0x0A~0x0F ‘a(chǎn)’~’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~0x09、 ’A’~’F’->0x0A~0x0F ‘a(chǎn)’~’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}; //轉為十六進制后的數(shù)組
int ch;
/*從txt中讀出字符保存到buffer數(shù)組*/
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
{
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;
}
二、python實現(xiàn)
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類型打印為
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數(shù)據(jù)轉為unsigned char的字符串
g.write(xHex) #write()函數(shù)參數(shù)只能為str類型
strY = '' #strY 置為空字符串
Struct模塊的pack和unpack不在這里介紹。
有這個測試的原因是因為,在嵌入式中讀出串口攝像頭返回的數(shù)據(jù)并在仿真模式下在控制臺打印了出來。目的把讀出來的數(shù)據(jù)寫成圖片文件,驗證攝像頭返回的數(shù)據(jù)是否正確。雖然本嵌入式中沒有文件系統(tǒng),但可以直接在嵌入式中的仿真模式下直接寫成圖片文件就可以驗證,
/*保存為二進制圖片*/
fp = fopen("D:\\photo.jpg","wb");
if(fp != NULL)
{
fwrite(photoHEX,1,len,fp);
}
fclose(fp);
fp = NULL;
結果兜了一大圈來實現(xiàn)。吐槽一下思維僵化。
總結
以上是生活随笔為你收集整理的python写二进制文件_初学Python写二进制文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华强北耳机修改序列号|支持中英文|自定义
- 下一篇: 用python画篮球场_Python可视