我有一个char[]类型的值,例如 "0x1800785",但是我想将其赋值给需要int类型的函数,如何将其转换为int类型?我搜索了一下但找不到答案。谢谢。
unsigned hexdec (const char *hex, const int s_hex);
在进行第一次转换之前,使用以下内容初始化用于转换的数组:
void init_hexdec ();
这里是Github链接: https://github.com/kevmuret/libhex/
我喜欢@radhoo的解决方案,对于小系统非常高效。可以修改该解决方案以将十六进制转换为int32_t(因此是有符号值)。
/**
* hex2int
* take a hex string and convert it to a 32bit number (max 8 hex digits)
*/
int32_t hex2int(char *hex) {
uint32_t val = *hex > 56 ? 0xFFFFFFFF : 0;
while (*hex) {
// get current character then increment
uint8_t byte = *hex++;
// transform hex character to the 4bit equivalent number, using the ascii table indexes
if (byte >= '0' && byte <= '9') byte = byte - '0';
else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10;
else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10;
// shift 4 to make space for new digit, and add the 4 bits of the new digit
val = (val << 4) | (byte & 0xF);
}
return val;
}
该
uint32_t val = *hex > 56 ? 0xFFFFFFFF : 0;
未受到对于畸形字符串的保护。
这里是基于"sairam singh"的解决方案的一个解决方案。那个答案是一对一的解决方案,而这个解决方案将两个ASCII半字节合并为一个字节。
// Assumes input is null terminated string.
//
// IN OUT
// -------------------- --------------------
// Offset Hex ASCII Offset Hex
// 0 0x31 1 0 0x13
// 1 0x33 3
// 2 0x61 A 1 0xA0
// 3 0x30 0
// 4 0x00 NULL 2 NULL
int convert_ascii_hex_to_hex2(char *szBufOut, char *szBufIn) {
int i = 0; // input buffer index
int j = 0; // output buffer index
char a_byte;
// Two hex digits are combined into one byte
while (0 != szBufIn[i]) {
// zero result
szBufOut[j] = 0;
// First hex digit
if ((szBufIn[i]>='A') && (szBufIn[i]<='F')) {
a_byte = (unsigned int) szBufIn[i]-'A'+10;
} else if ((szBufIn[i]>='a') && (szBufIn[i]<='f')) {
a_byte = (unsigned int) szBufIn[i]-'a'+10;
} else if ((szBufIn[i]>='0') && (szBufIn[i]<='9')) {
a_byte = (unsigned int) szBufIn[i]-'0';
} else {
return -1; // error with first digit
}
szBufOut[j] = a_byte << 4;
// second hex digit
i++;
if ((szBufIn[i]>='A') && (szBufIn[i]<='F')) {
a_byte = (unsigned int) szBufIn[i]-'A'+10;
} else if ((szBufIn[i]>='a') && (szBufIn[i]<='f')) {
a_byte = (unsigned int) szBufIn[i]-'a'+10;
} else if ((szBufIn[i]>='0') && (szBufIn[i]<='9')) {
a_byte = (unsigned int) szBufIn[i]-'0';
} else {
return -2; // error with second digit
}
szBufOut[j] |= a_byte;
i++;
j++;
}
szBufOut[j] = 0;
return 0; // normal exit
}
这里是C语言的一个简单解决方案 #include<stdio.h> int main (void) { unsigned char t=0x8A; int c = (t>>4 )*16 + (t & 0x0F); printf("0X8A的整数值为: %d\n", c); }
这是我将十六进制转换为整数的解决方案。非常简单!要处理十六进制数组,可以使用“for循环”。
#include<stdio.h>
int main (void)
{
unsigned char t=0x8A;
int c = (t>>4 )*16 + (t & 0x0F);
printf("The integrate value of 0X8A is : %d\n", c);
}
该值应为138。
我知道这个问题很老了,但我认为解决方案看起来太复杂了。在VB中尝试这个:
Public Function HexToInt(sHEX as String) as long
Dim iLen as Integer
Dim i as Integer
Dim SumValue as Long
Dim iVal as long
Dim AscVal as long
iLen = Len(sHEX)
For i = 1 to Len(sHEX)
AscVal = Asc(UCase(Mid$(sHEX, i, 1)))
If AscVal >= 48 And AscVal <= 57 Then
iVal = AscVal - 48
ElseIf AscVal >= 65 And AscVal <= 70 Then
iVal = AscVal - 55
End If
SumValue = SumValue + iVal * 16 ^ (iLen- i)
Next i
HexToInt = SumValue
End Function