且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

十六进制ASCII字符串转换

更新时间:2023-02-03 08:29:19

您需要采取2(十六进制)字符的同时...然后计算int值
之后,使焦炭转化喜欢...

you need to take 2 (hex) chars at the same time... then calculate the int value and after that make the char conversion like...

字符D =(焦炭)的intValue;

在十六进制字符串的每个2chars做到这一点。

do this for every 2chars in the hex string

这工作,如果该字符串的字符只有0-9A-F:

this works if the string chars are only 0-9A-F:

#include <stdio.h>
#include <string.h>

int hex_to_int(char c){
        int first = c / 16 - 3;
        int second = c % 16;
        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];
                }
        }
}