C/C++:将 char 中的十六进制值转换为整数

C/C++: Converting hexadecimal value in char to integer(C/C++:将 char 中的十六进制值转换为整数)

本文介绍了C/C++:将 char 中的十六进制值转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将十六进制值存储为字符:

I have hexadecimal values stored as characters:

char A = '0';
char B = '6';
char C = 'E';

...我需要将它们转换为整数.我知道'atoi',但这仅适用于十进制编码的字符值.有没有类似的功能?

... I need them coverted to integers. I know 'atoi', but this only works for decimal coded char values. Any similar function?

推荐答案

你可以试试strtol.但是 strtol 需要一个以 0 结尾的 char *,所以:

You could try strtol. But strtol needs a 0 terminated char *, so:

long x = strtol((char[]){A, 0}, NULL, 16);

这篇关于C/C++:将 char 中的十六进制值转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C/C++:将 char 中的十六进制值转换为整数

基础教程推荐