将16进制字符转换成10进制
2015-11-28 14:26
513 查看
## 题目要求 ## 将16进制字符转换成10进制,在字符串不是16进制字符串时抛出NumberFormatException异常。
public class display { public static void main(String[] args) { System.out.println(parseHex("A5")); System.out.println(parseHex("FAA")); System.out.println(parseHex("A5")); System.out.println(parseHex("ABC")); System.out.println(parseHex("J")); } public static int parseHex(String hexString) { int value = convertHexToDec(hexString.charAt(0)); for (int i = 1; i < hexString.length(); i++) { value = value * 16 + hexString.charAt(i) - '0'; } return value; } static int convertHexToDec(char ch) { if (ch == 'A') { return 10; } else if (ch == 'B') { return 11; } else if (ch == 'C') { return 12; } else if (ch == 'D') { return 13; } else if (ch == 'E') { return 14; } else if (ch == 'F') { return 15; } else if (ch <= '9' && ch >= '0') return ch - '0'; else throw new NumberFormatException("Illegal character: " + ch); } }
## 结果 ##
相关文章推荐
- char * const cp | const char * p | char const * p 区别
- va_start和va_end使用详解
- ThinkPHP3.2.3生成多张缩略图
- MR调度算法
- 164 Using the LIST command in Recovery Manager (RMAN), which two pieces of information from the RMAN
- ActionBar的TabListener与Fragment
- android 5.X之使用Palette
- 代码整洁之道(五)---------------------数据结构与对象
- 警告:MySQL-server-5.5.46-1.linux2.6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
- android之GridView实现九宫格布局
- 安装fedora后要安装的软件
- LeetCode---Multiply Strings
- Nginx (一) 基础入门
- nyoj 36 最长公共子序列【lcs&&dp】
- Linux下DIR,dirent,stat等结构体详解
- 杭电-1304A Contesting Decision(结构体)
- html中输入控件的元素
- Android项目重构之路:架构篇
- List简单升序,降序排列 linq
- emacs编辑器的使用