Unicodeでいわゆる全角文字のAからZは0xFF21から0xFF3Aに、aからzは0xFF41から0xFF5Aに、
0から9は0xFF10から0xFF19に割り当てられています。
半角文字のAからZは0x0041から0x005Aに、aからzは0x0061から0x007Aに、0から9は0x0030から0x0039に割り当てられています。
この関係で変換すればよいと思います。
以下がその例です。
ハイフン(に見える)文字は何種類かありますので、調べられたもののみで対応しました。
public class halfchar {
private static final byte HyphenBytes[] =
{ 0x22, 0x12, 0x00, 0x2D, 0x20, 0x10, 0x30, (byte)0xFC, (byte)0xFF, 0x0D };
public static void main(String args[]) {
String Str, HyphenStr;
char Chr;
StringBuffer StrBuff = new StringBuffer();
byte b[];
int i, j, HyphenLen;
try {
HyphenStr = new String(HyphenBytes, "UTF-16BE");
HyphenLen = HyphenStr.length();
for (i = 0; i < args[0].length(); i ++) {
Chr = args[0].charAt(i);
Str = Character.toString(Chr);
if (Str.matches("[[A-Z][a-z][0-9]]")) {
b = Str.getBytes("UTF-16BE");
b[0] = 0;
b[1] += (byte)0x20;
StrBuff.append(new String(b, "UTF-16BE"));
}
else {
for (j = 0; j < HyphenLen; j ++)
if (HyphenStr.charAt(j) == Chr) break;
if (j == HyphenLen) StrBuff.append(Chr);
else StrBuff.append("-");
};
};
System.out.println(StrBuff.toString());
} catch (Exception e) { System.out.println(e.toString()); };
};
}
お礼
超クールな方法を教えていただきありがとうございます。文字コード体系を生かした方法という発想は全くありませんでした。大変参考になりました。