十六进制ASCII字符串转换 [英] Hex to ascii string conversion

查看:146
本文介绍了十六进制ASCII字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个十六进制字符串,并希望将其转换为C. ASCII字符串我怎样才能做到这一点?

i have an hex string and want it to be converted to ascii string in C. How can i accomplish this??

推荐答案

您需要采取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];
                }
        }
}

这篇关于十六进制ASCII字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆