码迷,mamicode.com
首页 > 编程语言 > 详细

Java 字符串类型16进制字面值转成字面值字节数据工具类

时间:2014-12-25 20:34:11      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:android

private static byte hexValueOf(String hex) {
		byte result = 0;
		if (hex.charAt(0) == '0'
				&& (hex.charAt(1) == 'X' || hex.charAt(1) == 'x')) {
			for (int i = 2; i < hex.length(); i++) {
				char temp = hex.charAt(i);
				if (i == 2) {
					if (temp == '0' || temp == '1' || temp == '2'
							|| temp == '3' || temp == '4' || temp == '5'
							|| temp == '6' || temp == '7') {

						result += Byte.valueOf(temp + "") * 16;
					}
				} else {
					if (temp == '0' || temp == '1' || temp == '2'
							|| temp == '3' || temp == '4' || temp == '5'
							|| temp == '6' || temp == '7') {

						result += Byte.valueOf(temp + "");
					}

					if (temp == 'A' || temp == 'a' || temp == 'B'
							|| temp == 'b' || temp == 'C' || temp == 'c'
							|| temp == 'D' || temp == 'd' || temp == 'E'
							|| temp == 'e' || temp == 'F' || temp == 'f') {
						switch (temp) {
						case 'A':
							result += Byte.valueOf(10 + "");
							break;
						case 'a':
							result += Byte.valueOf(10 + "");
							break;
						case 'B':
							result += Byte.valueOf(11 + "");
							break;
						case 'b':
							result += Byte.valueOf(11 + "");
							break;
						case 'C':
							result += Byte.valueOf(12 + "");
							break;
						case 'c':
							result += Byte.valueOf(12 + "");
							break;
						case 'D':
							result += Byte.valueOf(13 + "");
							break;
						case 'd':
							result += Byte.valueOf(13 + "");
							break;
						case 'E':
							result += Byte.valueOf(14 + "");
							break;
						case 'e':
							result += Byte.valueOf(14 + "");
							break;
						case 'F':
							result += Byte.valueOf(15 + "");
							break;
						case 'f':
							result += Byte.valueOf(15 + "");
							break;
						default:
							break;
						}
					}

				}

			}
		}

		return result;

	}



System.out.println("<<<<<"+(hexValueOf("0X7f")));
  System.out.println("<<<<<"+(hexValueOf("0X7f")==127));


Java 字符串类型16进制字面值转成字面值字节数据工具类

标签:android

原文地址:http://blog.csdn.net/u014600432/article/details/42150695

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!