美文网首页
iOS hex文件转bin文件

iOS hex文件转bin文件

作者: Snow_L | 来源:发表于2021-04-15 13:37 被阅读0次

hex文件转换bin文件有两种方式:

1.通过软件的方式,软件的方式我是通过软件J-Flash来转换的。具体操作流程如下:

(1)打开J-Flash选择Create a new project。

(2)把hex文件拖入J-Flash 。

(3)找到hex文件对应的结束的最后一位的位置。

(4)选择Save data file as 保存类型选bin类型,然后点击保存之后弹出Enter address range框 start address 保持不变,End address 输入你想要转换文件的结束地址,然后点击OK hex转bin文件转换成功。

2.通过到代码的方式。转换的代码如下:

//

//  LBHexToBin.m

//

//  Created by lingbing on 2020/9/18.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interface LBHexToBin : NSObject

+ (NSData*)convert:(NSData*)hex;

@end

NS_ASSUME_NONNULL_END

//

//  LBHexToBin.m

//

//  Created by lingbing on 2020/9/18.

//

#import "LBHexToBin.h"

@implementation LBHexToBin

+ (constByte)ascii2char:(constByte*)ascii

{

 if(*ascii >='A')

 return*ascii -0x37;

 if(*ascii >='0')

 return*ascii -'0';

 return-1;

}

+ (constByte)readByte:(constByte*)pointer

{

    Bytefirst = [LBHexToBinascii2char:pointer];

    Bytesecond = [LBHexToBinascii2char:pointer +1];

 return(first <<4) | second;

}

+ (constUInt16)readAddress:(constByte*)pointer

{

    Bytemsb = [LBHexToBinreadByte:pointer];

    Bytelsb = [LBHexToBinreadByte:pointer +2];

 return(msb <<8) | lsb;

}

+ (NSUInteger)calculateBinLength:(NSData*)hex

{

 if(hex ==nil|| hex.length==0)

    {

 return0;

    }

    NSUIntegerbinLength =0;

 constNSUIntegerhexLength = hex.length;

 constByte* pointer = (constByte*)hex.bytes;

    UInt32lastBaseAddress =0;

 do

    {

 constBytesemicollon = *pointer++;

        // Validate - each line of the file must have a semicollon as a firs char

 if(semicollon !=':')

        {

 return0;

        }

 constUInt8reclen = [LBHexToBinreadByte:pointer]; pointer +=2;

 constUInt16offset = [LBHexToBinreadAddress:pointer]; pointer +=4;

 constUInt8rectype = [LBHexToBinreadByte:pointer]; pointer +=2;

 switch(rectype) {

 case0x04: {

                // Only consistent hex files are supported. If there is a jump to non-following ULBA address skip the rest of the file

 constUInt32newULBA = [LBHexToBinreadAddress:pointer];

 if(binLength >0&& newULBA != (lastBaseAddress >>16) +1)

 returnbinLength;

                lastBaseAddress = newULBA <<16;

 break;

            }

 case0x02: {

                // The same with Extended Segment Address. The calculated ULBA must not be greater than the last one + 1

 constUInt32newSBA = [LBHexToBinreadAddress:pointer] <<4;

 if(binLength >0&& (newSBA >>16) != (lastBaseAddress >>16) +1)

 returnbinLength;

                lastBaseAddress = newSBA;

 break;

            }

 case0x00:

                // If record type is Data Record (rectype = 0), add it's length (only it the address is >= 0x1000, MBR is skipped)

 if(lastBaseAddress + offset >=0x1000)

                    binLength += reclen;

 default:

 break;

        }

        pointer += (reclen <<1);  // Skip the data when calculating length

        pointer +=2;  // Skip the checksum

        // Skip new line

 if(*pointer =='\r') pointer++;

 if(*pointer =='\n') pointer++;

}while(pointer != hex.bytes+ hexLength);

 returnbinLength;

}

+ (NSData*)convert:(NSData*)hex

{

 constNSUIntegerbinLength = [LBHexToBincalculateBinLength:hex];

 constNSUIntegerhexLength = hex.length;

 constByte* pointer = (constByte*)hex.bytes;

    NSUIntegerbytesCopied =0;

    UInt32lastBaseAddress =0;

Byte* bytes =malloc(sizeof(Byte) * binLength);

    Byte* output = bytes;

 do

    {

 constBytesemicollon = *pointer++;

        // Validate - each line of the file must have a semicollon as a firs char

 if(semicollon !=':')

        {

            free(bytes);

 returnnil;

        }

 constUInt8reclen = [LBHexToBinreadByte:pointer]; pointer +=2;

 constUInt16offset = [LBHexToBinreadAddress:pointer]; pointer +=4;

 constUInt8rectype = [LBHexToBinreadByte:pointer]; pointer +=2;

 switch(rectype) {

 case0x04: {

 constUInt32newULBA = [LBHexToBinreadAddress:pointer]; pointer +=4;

 if(bytesCopied >0&& newULBA != (lastBaseAddress >>16) +1)

 return[NSDatadataWithBytesNoCopy:byteslength:bytesCopied];

                lastBaseAddress = newULBA <<16;

 break;

            }

 case0x02: {

 constUInt32newSBA = [LBHexToBinreadAddress:pointer] <<4; pointer +=4;

 if(bytesCopied >0&& (newSBA >>16) != (lastBaseAddress >>16) +1)

 return[NSDatadataWithBytesNoCopy:byteslength:bytesCopied];

                lastBaseAddress = newSBA;

 break;

            }

 case0x00:

                // If record type is Data Record (rectype = 0), copy data to output buffer

                // Skip data below 0x1000 address (MBR)

 if(lastBaseAddress + offset >=0x1000)

                {

 for(inti =0; i < reclen; i++)

                    {

                        *output++ = [LBHexToBinreadByte:pointer]; pointer +=2;

                        bytesCopied++;

                    }

                }

 else

                {

                    pointer += (reclen <<1);  // Skip the data

                }

 break;

 default:

                pointer += (reclen <<1);  // Skip the irrelevant data

 break;

        }

        pointer +=2;  // Skip the checksum

        // Skip new line

 if(*pointer =='\r') pointer++;

 if(*pointer =='\n') pointer++;

}while(pointer != hex.bytes+ hexLength);

 return[NSDatadataWithBytesNoCopy:byteslength:bytesCopied];

}

@end

相关文章

  • iOS hex文件转bin文件

    hex文件转换bin文件有两种方式: 1.通过软件的方式,软件的方式我是通过软件J-Flash来转换的。具体操作流...

  • java实现 hex 文件转 bin 文件

    hex 文件格式是可以烧写到单片机中,被单片机执行的一种文件格式,生成Hex文件的方式有很多种,可以通过不同的编译...

  • Hex、Bin文件解读

    做过单片机开发的朋友应该对hex和bin文件都比较眼熟了,但你是否真正理解这两种文件的区别和联系吗?今天我想总结一...

  • Hex文件转换成Bin文件

    一、转换工具下载地址 链接:https://pan.baidu.com/s/1i0Un8BcQjg66ZIpm5Z...

  • Shell学习

    基础 头文件 #!/bin/bash 或者#!/bin/sh #!约定的标记,如IOS的Import 执行文件时需...

  • hex、bin、img

    bin和img都是原始二进制文件,放到内存指定位置可以直接运行, hex文件是使用ascii表示的具有一定格式的文...

  • 单片机下载文件:HEX文件和BIN文件的区别

    单片机程序写好之后,我们都要把程序下载到单片机的内存中,单片机才会按照程序员的逻辑执行命令实现功能。之前也讲过下载...

  • iOS OC DES-hex加密

    加解密流程:content -> hex -> des -> hex -> content 引入头文件: DES加...

  • Hex编码的文件进行解码

    Hex编码的文件进行解码

  • python各种进制转换

    python转二进制bin('10')python转八进制oct('10')python转十六进制hex('10'...

网友评论

      本文标题:iOS hex文件转bin文件

      本文链接:https://www.haomeiwen.com/subject/typdlltx.html