關於Hex檔案的解析和修改應用

2021-10-10 21:15:27 字數 2190 閱讀 9786

目錄前言

一、hex是什麼?

二、使用步驟

1.引入hexlexer類

2.呼叫類

最近研究了一下hex檔案的用法。主要用途是配合stvp進行量產工具的開發應用。因為涉及到常量的修改不想編譯和修改或者應用再無法修改原始檔案的地方 可以通過提前記錄對應的常量偏移來進行修改。

需求:修改指定偏移位置的hex檔案 並將此行的記錄校驗修正。

下面是我對網上提供的一些原始碼基礎上進行的**修改

這裡是我的試驗工程**有興趣可以查閱

網上對於hex檔案的說明和分析已經很詳細了。這裡就不多囉嗦了。

hexlexer.cpp

#pragma once

#include "stdafx.h"

#include "hexlexer.h"

//#include //using namespace std;

//獲取記錄標識

hex::hex(char mark)

hex::~hex()

char hex::getrecordmark()

//獲取每條記錄的長度

size_t hex::getrecordlength()

else }

//獲取裝載偏移

char* hex::getloadoffset()

offset[4] = '\0';

m_ploadoffset = offset;

offset = null;

} return m_ploadoffset;

}//獲取記錄型別

char* hex::getrecordtype()

return m_precordtype;

}//獲取資料

char* hex::getdata()

data[len * 2] = '\0';

m_pdata = data;

data = null;

} return m_pdata;

}//獲取校驗和

char* hex::getchecksum()

return m_pchecksum;

}//解析hex檔案中的每一條記錄

void hex::parserecord(char ch)

if ((buf_len == (getrecordlength() + 5) * 2 - 1))

checksum &= 0x00ff;//取計算結果的低8位

if (checksum == 0)//checksum為0說明接收的資料無誤

//(13-1)*2 24+9

int noffset = (nmodifyoffset - 1) * 2 + 9;//計算偏移位置

if (!strcmp(m_ploadoffset, coffsetdata))//對比偏移資料是否一致 }

void hex::modifyhex(char* srcpath, char* despath)

} fclose(stdout);

fclose(stdin);

}#if 0

int main(int argc, char *ar**)

fclose(stdout);

fclose(stdin);

return 0;

}#endif

hexlexer.h

#pragma once

#ifndef _hexlexer_h_

#define _hexlexer_h_

#include #include #include /*

intel hex檔案解析器v1.0

hex檔案的格式如下:

recordmark recordlength loadoffset recordtype data checksum

在intel hex檔案中,recordmark規定為「:」

*/#pragma warning(disable:4996)

#define max_buffer_size 43

class hex

;#endif

hex hex(':');

hex.modifyhex("in.hex", "out.hex");

hex檔案的解析 keil

在你做公升級的時候你會用到hex檔案的載入,所以對hex檔案的解析很重要。hex整個檔案以行為單位,每行以冒號開頭,內容全部為16進製製碼 以ascii碼形式顯示 格式為 0x3a 資料長度 1byte 資料位址 2byte 資料型別 1byte 資料 nbyte 校驗 1byte 0x0d 0x0...

關於鏈結檔案和hex檔案的一些小總結

在除錯某晶元平台時,希望將flash上的一片地方採用絕對位址定位定乙個陣列。於是修改了鏈結檔案,當時的鏈結檔案如下 memory eflash rodata eflash text eflash 0xbebe gjtest 0x408000 jtest data at end text ram bs...

HEX和BIN檔案的區別

hex 檔案和 bin 檔案的區別,這兩個檔案裡都包含 cpu可以執行的機器碼,只是 hex 比bin 多一些資訊,就是機器碼的儲存 位址。這些儲存位址實際是給燒寫程式的軟體使用的。如果我們燒寫 hex 檔案到板卡裡 flash 儲存器 燒寫軟體就不會問我們儲存位址,因為這個檔案裡已經包含了這類資訊...