# 16进制字符串如何转化为数字

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/15129.html | 发布：2022-12-30 | 更新：2022-12-30 | 分类：建站教程、投稿分享

#include <stdio.h>  
#include <string.h>  
typedef unsigned long DWORD;  
// 函数功能：16进制字符串转化为数字  
// 函数返回：0不是16进制数，1是十六进制数  
bool HexstrToHex(char *str, DWORD &dwVal)  
{  
 int nLen;  
 int i;  
 int pow;  
 DWORD s;  
 char ch;   
 char *p;  
 nLen = strlen(str);  
 p = str + nLen;  
 if (nLen > 8 || nLen == 0)  
 return false;  
 s = 0;  
 pow = 1;  
 for(i = nLen; i > 0; i --)  
 {  
 ch = *(--p);  
 if ((ch >= 'a') && (ch <= 'f'))  
 ch = ch - 'a' + 10;  
 else if ((ch >= 'A') && (ch <= 'F'))  
 ch = ch - 'A' + 10;  
 else if ((ch >= '0') && (ch <= '9'))  
 ch = ch - '0';  
 else  
 return false;  
 s += ch * pow;  
 pow *= 16;  
 }  
 dwVal = s;  
 return 1;  
}  
void main()  
{  
 char str[] = "a0";  
 DWORD dwVal;  
 if (!HexstrToHex(str, dwVal))  
 printf("failed./n");  
 else  
 printf("%x/n", dwVal);  
}

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
