JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

遍历UTF8中的小写转大写后长度大于一的字符

wys521 2024-12-02 18:18:34 精选教程 18 ℃ 0 评论

遍历UTF8的三字节字符,查找出字符大写后长度>1的字符。

Unicode符号范围         |        UTF-8编码方式
------------------------+---------------------------------------------
0000 0000-0000 007F | 0xxxxxxx(单字节)
0000 0080-0000 07FF | 110xxxxx 10xxxxxx(2字节)
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx(3字节)
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx(4字节)

结果如下:

|char_utf8|char_unicode|char_lower|char_upper|
|---------|------------|----------|----------|
|E1BA96   |1e96        |?         |H?        |
|E1BA97   |1e97        |?         |T?        |
|E1BA98   |1e98        |?         |W?        |
|E1BA99   |1e99        |?         |Y?        |
|E1BA9A   |1e9a        |?         |A?        |
|E1BD90   |1f50        |?         |Υ?        |
|E1BD92   |1f52        |?         |Υ??       |
|E1BD94   |1f54        |?         |Υ??       |
|E1BD96   |1f56        |?         |Υ??       |
|E1BE80   |1f80        |?         |?Ι        |
|E1BE81   |1f81        |?         |?Ι        |
|E1BE82   |1f82        |?         |?Ι        |
|E1BE83   |1f83        |?         |?Ι        |
|E1BE84   |1f84        |?         |?Ι        |
|E1BE85   |1f85        |?         |?Ι        |
|E1BE86   |1f86        |?         |?Ι        |
|E1BE87   |1f87        |?         |?Ι        |
|E1BE88   |1f88        |?         |?Ι        |
|E1BE89   |1f89        |?         |?Ι        |
|E1BE8A   |1f8a        |?         |?Ι        |
|E1BE8B   |1f8b        |?         |?Ι        |
|E1BE8C   |1f8c        |?         |?Ι        |
|E1BE8D   |1f8d        |?         |?Ι        |
|E1BE8E   |1f8e        |?         |?Ι        |
|E1BE8F   |1f8f        |?         |?Ι        |
|E1BE90   |1f90        |?         |?Ι        |
|E1BE91   |1f91        |?         |?Ι        |
|E1BE92   |1f92        |?         |?Ι        |
|E1BE93   |1f93        |?         |?Ι        |
|E1BE94   |1f94        |?         |?Ι        |
|E1BE95   |1f95        |?         |?Ι        |
|E1BE96   |1f96        |?         |?Ι        |
|E1BE97   |1f97        |?         |?Ι        |
|E1BE98   |1f98        |?         |?Ι        |
|E1BE99   |1f99        |?         |?Ι        |
|E1BE9A   |1f9a        |?         |?Ι        |
|E1BE9B   |1f9b        |?         |?Ι        |
|E1BE9C   |1f9c        |?         |?Ι        |
|E1BE9D   |1f9d        |?         |?Ι        |
|E1BE9E   |1f9e        |?         |?Ι        |
|E1BE9F   |1f9f        |?         |?Ι        |
|E1BEA0   |1fa0        |?         |?Ι        |
|E1BEA1   |1fa1        |?         |?Ι        |
|E1BEA2   |1fa2        |?         |?Ι        |
|E1BEA3   |1fa3        |?         |?Ι        |
|E1BEA4   |1fa4        |?         |?Ι        |
|E1BEA5   |1fa5        |?         |?Ι        |
|E1BEA6   |1fa6        |?         |?Ι        |
|E1BEA7   |1fa7        |?         |?Ι        |
|E1BEA8   |1fa8        |?         |?Ι        |
|E1BEA9   |1fa9        |?         |?Ι        |
|E1BEAA   |1faa        |?         |?Ι        |
|E1BEAB   |1fab        |?         |?Ι        |
|E1BEAC   |1fac        |?         |?Ι        |
|E1BEAD   |1fad        |?         |?Ι        |
|E1BEAE   |1fae        |?         |?Ι        |
|E1BEAF   |1faf        |?         |?Ι        |
|E1BEB2   |1fb2        |?         |?Ι        |
|E1BEB3   |1fb3        |?         |ΑΙ        |
|E1BEB4   |1fb4        |?         |?Ι        |
|E1BEB6   |1fb6        |?         |Α?        |
|E1BEB7   |1fb7        |?         |Α?Ι       |
|E1BEBC   |1fbc        |?         |ΑΙ        |
|E1BF82   |1fc2        |?         |?Ι        |
|E1BF83   |1fc3        |?         |ΗΙ        |
|E1BF84   |1fc4        |?         |?Ι        |
|E1BF86   |1fc6        |?         |Η?        |
|E1BF87   |1fc7        |?         |Η?Ι       |
|E1BF8C   |1fcc        |?         |ΗΙ        |
|E1BF92   |1fd2        |?         |Ι??       |
|E1BF93   |1fd3        |?         |Ι??       |
|E1BF96   |1fd6        |?         |Ι?        |
|E1BF97   |1fd7        |?         |Ι??       |
|E1BFA2   |1fe2        |?         |Υ??       |
|E1BFA3   |1fe3        |?         |Υ??       |
|E1BFA4   |1fe4        |?         |Ρ?        |
|E1BFA6   |1fe6        |?         |Υ?        |
|E1BFA7   |1fe7        |?         |Υ??       |
|E1BFB2   |1ff2        |?         |?Ι        |
|E1BFB3   |1ff3        |?         |ΩΙ        |
|E1BFB4   |1ff4        |?         |?Ι        |
|E1BFB6   |1ff6        |?         |Ω?        |
|E1BFB7   |1ff7        |?         |Ω?Ι       |
|E1BFBC   |1ffc        |?         |ΩΙ        |
|EFAC80   |fb00        |?         |FF        |
|EFAC81   |fb01        |?         |FI        |
|EFAC82   |fb02        |?         |FL        |
|EFAC83   |fb03        |?         |FFI       |
|EFAC84   |fb04        |?         |FFL       |
|EFAC85   |fb05        |?         |ST        |
|EFAC86   |fb06        |?         |ST        |
|EFAC93   |fb13        |?         |??        |
|EFAC94   |fb14        |?         |??        |
|EFAC95   |fb15        |?         |??        |
|EFAC96   |fb16        |?         |??        |
|EFAC97   |fb17        |?         |??        |

Java代码如下:

/**
 * 
 */
package com.gdxsoft.temp;

import java.io.UnsupportedEncodingException;

/**
 * 遍历UTF8中的小写转大写后长度大于一的字符
 * @author admin
 *
 */
public class FindLowerNotEqualsUpperChar {
	public static void main(String[] args) throws UnsupportedEncodingException {

		FindLowerNotEqualsUpperChar o = new FindLowerNotEqualsUpperChar();
		o.findLowerNotEqualsUpperChar();
	}

	/**
	 * 遍历3字节的utf8
	 * @throws UnsupportedEncodingException
	 * 
	 */
	public void findLowerNotEqualsUpperChar() throws UnsupportedEncodingException {
		// Unicode符号范围     | UTF-8编码方式
		// 0000 0000-0000 007F | 0xxxxxxx(单字节)
		// 0000 0080-0000 07FF | 110xxxxx 10xxxxxx(2字节)
		// 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx(3字节)
		// 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx(4字节)
		
		// 
		// 第零字节开始
		byte start = bit2byte("1110xxxx".replace("x", "0"));
		// 第零字节结束
		byte end = bit2byte("1110xxxx".replace("x", "1"));

		// 第1、第2字节开始
		byte extStart = bit2byte("10xxxxxx".replace("x", "0"));
		// 第1、第2字节结束
		byte extEnd = bit2byte("10xxxxxx".replace("x", "1"));

		int total = 0;

		StringBuilder sb = new StringBuilder();
		for (byte byte0 = start; byte0 <= end; byte0++) {
			for (byte byte1 = extStart; byte1 <= extEnd; byte1++) {
				for (byte byte2 = extStart; byte2 <= extEnd; byte2++) {
					// 3字节字符的二进制
					byte[] charBytes = new byte[3];
					charBytes[0] = byte0;
					charBytes[1] = byte1;
					charBytes[2] = byte2;

					// 转换为字符
					String newChar = new String(charBytes, "utf-8");
					// 大写
					String newCharUpper = newChar.toUpperCase();
					if (newChar.length() == newCharUpper.length()) {
						// 长度一致
						continue;
					}
					String hexUtf8 = this.bytes2hex(charBytes); // utf8 内码
					String hexUnicode = Integer.toHexString(newChar.codePointAt(0)); // unicode 内码
					sb.append(newChar + "\t" + newCharUpper + "\t" + hexUtf8 + "\t" + hexUnicode + "\n");
					total++;
				}
			}
		}
		System.out.println("total:" + total);
		System.out.println(sb);
	}

	/**
	 * 将二进制转换为字节
	 * 
	 * @param binary 二进制字符串
	 * @return
	 */
	private byte bit2byte(String binary) {
		byte result = 0;
		for (int i = binary.length() - 1, j = 0; i >= 0; i--, j++) {
			byte b = Byte.parseByte(binary.charAt(i) + "");
			if (b == 1) {
				result += b * Math.pow(2, j);
			}
		}
		return result;
	}

	/**
	 * 将byte数组转换为16进制字符串
	 * 
	 * @param b byte数组
	 * @return 16进制字符串
	 */
	private String bytes2hex(byte[] bytes) {
		String stmp = "";
		StringBuilder sb = new StringBuilder();
		for (int n = 0; n < bytes.length; n++) {
			stmp = (java.lang.Integer.toHexString(bytes[n] & 0XFF));
			if (stmp.length() == 1)
				sb.append("0" + stmp);
			else
				sb.append(stmp);
		}
		return sb.toString().toUpperCase();
	}

}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表