zoukankan      html  css  js  c++  java
  • 汉字转整数,比系统简单易用!a2iLxx (覆盖物 16十六进制,VC6亲测可用)请提供意见~

    #include "string.h"
    #define INVALID_VALUE_LXX	((1 << (8 * sizeof(int) -1)) - 1)	
    /*有符号整型最大值,假设越界将为0, and - is prioty of <<*/
    #define A2a(ch) (('a' <= (ch) && (ch) <= 'f') ? (ch) : ((ch) - 'A' + 'a'))
    
    bool is0to9(char ch)
    {
    	if ('0' <= ch && ch <= '9')
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    bool isatoF(char ch)
    {
    	if (('a' <= ch && ch <= 'f') || ('A' < ch && ch <= 'F'))
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    int a2iLxx(char* ch)
    {
    	int index = 0;
    	int retVal = 0;
    	bool flag = false;
    	if (NULL == ch)
    	{
    		return INVALID_VALUE_LXX;
    	}
    
    	/*Ox*/
    	if ('0' == ch[0] && ('x' == ch[1] || 'X' == ch[1]))
    	{
    		index = 2;
    		for (; ch[index] != ''; index++)
    		{
    			/* x * 2^y == x * 1 << y */
    			if (is0to9(ch[index]))
    			{
    				retVal = (retVal << 4) + ch[index] - '0';
    				continue;
    			}
    			
    			if (isatoF(ch[index]))
    			{
    				retVal = (retVal << 4) + 10 + A2a(ch[index]) - 'a';
    				continue;
    			}
    
    			return INVALID_VALUE_LXX;
    
    		}
    		return retVal;
    	}
    
    	/*10*/
    	index = 0;
    	if ('-' == ch[0])
    	{
    		/*负数*/
    		flag = true;
    		index = 1;
    	}
    
    	for (; ch[index] != ''; index++)
    	{
    		if (!is0to9(ch[index]))
    		{
    			return INVALID_VALUE_LXX;
    		}
    		retVal = retVal * 10 + ch[index] - '0';
    	}
    
    	if (flag)
    	{
    		return -retVal;
    	}
    	else
    	{
    		return retVal;
    	}
    }
    
    void main()
    {
    	char ch1[] = "123";
    	char ch2[] = "-234";
    	char ch3[] = "0";
    	char ch4[] = "0x123";
    	char ch5[] = "0xff";
    	int a = INVALID_VALUE_LXX;
    	char d1 = A2a('a');
    	char d2 = A2a('B');
    	/*调试查看值*/
    	a = a2iLxx(ch1);
    	a = a2iLxx(ch2);
    	a = a2iLxx(ch3);
    	a = a2iLxx(ch4);
    	a = a2iLxx(ch5);
    }

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Python 发送邮件
    python3 根据时间获取本月一号和月末日期
    docker搭建MediaWiki
    linux下僵尸进程的发现与处理
    rootkit后门检查工具RKHunter
    CentOS7安装Node_exporter(二进制)
    用JS获取地址栏参数的方法(超级简单)
    php读取目录及子目录下所有文件名的方法
    css input[type=file] 样式美化,input上传按钮美化
    APACHE REWRITE ? 匹配问号的写法
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4854596.html
Copyright © 2011-2022 走看看