简体   繁体   English

将文件中的十六进制数转换为ASCII

[英]Convert Hex num in a File to ASCII

I have a file which I have to open using fopen. 我有一个必须使用fopen打开的文件。 Now the issue with the file is its a hex file so when I open it I see the Hex numbers for example RG is 5247. Now when I read the file using ( fgets(line, sizeof(line), fd ) 现在文件的问题是它的十六进制文件,所以当我打开它时,我看到十六进制数字,例如RG是5247。现在当我使用( fgets(line, sizeof(line), fd )读取文件时

line[0] is 5
line[1] is 2
line[2] is 4
line[4] is 7. 

52 is the hex for char R and 47 is the hex for char G . 52char R的十六进制,而47char G的十六进制。 I want to obtain that. 我想得到那个。 I know I can use a lookuptable and this will work, but I was looking for a more different solutions. 我知道我可以使用lookuptable,这将起作用,但是我一直在寻找更多不同的解决方案。 Have tried a lot but to no avail. 尝试了很多,但无济于事。

Please help!! 请帮忙!!

  • convert hex to int 将十六进制转换为int
  • convert result to char similar to char res = (char)intValue; 将结果转换为类似于char res = (char)intValue;

code: 码:

// this works if the string chars are only  0-9, A-F 
// because of implemented mapping in `hex_to_int`

int hex_to_int(char c){
        int first = c / 16 - 3;//    1st is dec 48 = char 0
        int second = c % 16; //      10 in 1st16  5 in 2nd 16
        // decimal code of ascii char 0-9:48-57  A-E: 65-69
        // omit dec 58-64:  :,;,<,=,>,?,@
        // map first or second 16 range to 0-9 or 10-15
        int result = first*10 + second; 
        if(result > 9) result--;
        return result;
}

int hex_to_ascii(char c, char d){
        int high = hex_to_int(c) * 16;
        int low = hex_to_int(d);
        return high+low;
}

int main(){
        const char* st = "48656C6C6F3B";
        int length = strlen(st);
        int i;
        char buf = 0;
        for(i = 0; i < length; i++){
                if(i % 2 != 0){
                        printf("%c", hex_to_ascii(buf, st[i]));
                }else{
                        buf = st[i];
                }
        }
}

output: 输出:

Hello; 你好;

RUN SUCCESSFUL (total time: 59ms) 运行成功(总时间:59ms)

You can convert each pair of ASCII-encoded hexadecimal digits into a character. 您可以将每对ASCII编码的十六进制数字转换为一个字符。

Something like this: 像这样:

   unsigned int high = line[0] - 0x30;
   if (high > 9)
      high -= 7;
   unsigned int low  = line[1] - 0x30;
   if (low > 9)
      low -= 7;
   char c = (char) ((high << 4) | low);

Of course, you can optimize the code above, and you'll have to write a loop over the characters in the variable "line". 当然,您可以优化上面的代码,并且必须在变量“ line”中的字符上编写一个循环。

Also, if lower case letters are used, first they'll have to be converted to upper case. 另外,如果使用小写字母,则首先必须将它们转换为大写字母。 Like 喜欢

unsigned char ch = line[0];
if (islower(ch))
   ch = toupper(ch);
unsigned int high = ch - 0x30;
if (high > 9)
   high -= 7;
etc

I just read a small hex file and following C++ code worked as per your need: 我只是阅读了一个小十六进制文件,并且根据您的需要使用了以下C ++代码:

#include <iostream>
#include<fstream>
#include<deque>
#include<sstream>
#include<string>

char val(const std::string& s)
{
    int x;   
    std::stringstream ss;
    ss << std::hex << s;
    ss >> x;
    return static_cast<char> (x);
}

int main()
{

std::deque<char> deq;

char ch;
std::string s;

std::ifstream fin("input.hex");

while (fin >> std::skipws >> ch) {
    if(ch != ':') //Hex file begins with ':'
      deq.push_back(ch);
    if(deq.size() ==2)
    {
      s.push_back(deq.front());
      deq.pop_front();

      s.push_back(deq.front());
      deq.pop_front();

      std::cout<< s<<":"<<val(s)<<std::endl;;
      s.clear();
    }
}

 fin.close() ;

 return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM