十六进制到ascii字符串转换
我有一个十六进制字符串,希望它在C中转换为ascii字符串。我如何才能做到这一点???您需要同时使用2个(十六进制)字符。。。然后计算int值 在这之后,让字符转换像十六进制到ascii字符串转换,c,C,我有一个十六进制字符串,希望它在C中转换为ascii字符串。我如何才能做到这一点???您需要同时使用2个(十六进制)字符。。。然后计算int值 在这之后,让字符转换像 chard=(char)intValue 对十六进制字符串中的每2个字符执行此操作 如果字符串字符仅为0-9A-F: #include <stdio.h> #include <string.h> int hex_to_int(char c){ int first = c / 16 - 3;
chard=(char)intValue代码>
对十六进制字符串中的每2个字符执行此操作
如果字符串字符仅为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];
}
}
}
#包括
#包括
整数十六进制到整数(字符c){
int first=c/16-3;
整数秒=c%16;
int结果=第一个*10+秒;
如果(结果>9)结果--;
返回结果;
}
int十六进制到ascii(字符c,字符d){
整数高=十六进制到整数(c)*16;
int低=十六进制到整数(d);
返回高+低;
}
int main(){
const char*st=“48656C6F3B”;
整数长度=strlen(st);
int i;
char-buf=0;
对于(i=0;i
像字母i-o这样的少数字符无法转换为各自的ASCII字符。
类似于字符串“6631653064316f30723161”对应的fedora。但它给了费德拉
只需将hex_修改为_int()函数一点,它将适用于所有字符。
修改后的函数是
int hex_to_int(char c)
{
if (c >= 97)
c = c - 32;
int first = c / 16 - 3;
int second = c % 16;
int result = first * 10 + second;
if (result > 9) result--;
return result;
}
现在尝试一下,它将适用于所有字符。strtol()
是您的朋友。第三个参数是要转换的数字基
例如:
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main(int argc, char **argv)
{
long int num = 0;
long int num2 =0;
char * str. = "f00d";
char * str2 = "0xf00d";
num = strtol( str, 0, 16); //converts hexadecimal string to long.
num2 = strtol( str2, 0, 0); //conversion depends on the string passed in, 0x... Is hex, 0... Is octal and everything else is decimal.
printf( "%ld\n", num);
printf( "%ld\n", num);
}
#包括/*printf*/
#包括/*strtol*/
int main(int argc,字符**argv)
{
long int num=0;
长整数num2=0;
字符*str.=“f00d”;
char*str2=“0xf00d”;
num=strtol(str,0,16);//将十六进制字符串转换为长字符串。
num2=strtol(str2,0,0);//转换取决于传入的字符串,0x…是十六进制,0…是八进制,其他所有内容都是十进制。
printf(“%ld\n”,num);
printf(“%ld\n”,num);
}
如果我理解正确,您想知道如何将编码为十六进制字符串的字节转换为ASCII文本的形式,如“537461636B”将转换为“Stack”,在这种情况下,下面的代码应该可以解决您的问题
没有运行任何基准测试,但我认为这不是效率的顶峰
static char ByteToAscii(const char *input) {
char singleChar, out;
memcpy(&singleChar, input, 2);
sprintf(&out, "%c", (int)strtol(&singleChar, NULL, 16));
return out;
}
int HexStringToAscii(const char *input, unsigned int length,
char **output) {
int mIndex, sIndex = 0;
char buffer[length];
for (mIndex = 0; mIndex < length; mIndex++) {
sIndex = mIndex * 2;
char b = ByteToAscii(&input[sIndex]);
memcpy(&buffer[mIndex], &b, 1);
}
*output = strdup(buffer);
return 0;
}
static char ByteToAscii(const char*input){
char singleChar,out;
memcpy(&singleChar,input,2);
sprintf(&out,“%c”,(int)strtol(&singleChar,NULL,16));
返回;
}
int HexStringToAscii(常量字符*输入,无符号整数长度,
字符**输出){
int mIndex,sIndex=0;
字符缓冲区[长度];
对于(mIndex=0;mIndex
十六进制字符串(例如:“F00BA4”
)不是ascii字符串的特例吗?或者正在使用EBCDIC?:)@YVAN no.首先,使用itoa()
进行转换是错误的atoi()
只接受十进制输入atof
接受十六进制(需要以上文第二个示例中的“0x”
开头,并且仅适用于C99或更高版本(如果需要的话))