昭和、平成、と経て~、令和になったしね、和の心!はい、日本に和の心が戻ったのか?どうもボクです。
というわけで、「半角カナ」の厄介なこと、厄介なこと、では、レッツトライ~。
半角カナとは
Wikipediaさん~!
んで、

と。
つまり、 『゙』『゚』のような濁音や半濁音の他にも、
- ・
- 「
- 」
- 、
- 。
- ー
は、1文字として扱われると。なので、全角『ガ』は1文字だけど、半角『ガ』だと、『カ』+『゙』の2文字として扱われるという(涙)。
何が問題だったのか
あるサイズまで、文字列を0埋めして、その文字列のbyte配列を設定する必要があったんだけど、「半角カナ」の『濁音』『半濁音』が影響して文字数が変わってくるので上手くいかないと。
ちなみに、
package main;
public class TestKana {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
int strByteSize = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑをん".getBytes().length;
int strLenSize ="あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑをん".length();
String zenkakuKana = "レイワ、チョベリバ~!ピーパッパラッポ";
String hankakuKana = "レイワ、チョベリバ~!ピーパッパラッポ";
boolean isByte = true;
System.out.println("バイト数: " + strByteSize);
System.out.println(zenkakuKana.getBytes().length);
System.out.println(hankakuKana.getBytes().length);
System.out.println(lpadding(zenkakuKana, strByteSize, isByte));
System.out.println(lpadding(hankakuKana, strByteSize, isByte));
System.out.println(lpadding(zenkakuKana, strByteSize, isByte).length());
System.out.println(lpadding(hankakuKana, strByteSize, isByte).length());
System.out.println(lpadding(zenkakuKana, strByteSize, isByte).getBytes().length);
System.out.println(lpadding(hankakuKana, strByteSize, isByte).getBytes().length);
isByte = false;
System.out.println("文字数: " + strLenSize);
System.out.println(zenkakuKana.length());
System.out.println(hankakuKana.length());
System.out.println(lpadding(zenkakuKana, strLenSize, isByte));
System.out.println(lpadding(hankakuKana, strLenSize, isByte));
System.out.println(lpadding(zenkakuKana, strLenSize, isByte).length());
System.out.println(lpadding(hankakuKana, strLenSize, isByte).length());
System.out.println(lpadding(zenkakuKana, strLenSize, isByte).getBytes().length);
System.out.println(lpadding(hankakuKana, strLenSize, isByte).getBytes().length);
}
public static String lpadding(String target, int size, boolean flg) {
// return String.format(("%"+ (size - target.getBytes().length) + "s"), target);
// return String.format(("%"+ (size - target.length()) + "s"), target);
if (flg) {
return String.format(("%"+ (size - target.getBytes().length) + "s"), target);
} else {
return String.format(("%"+ (size - target.length()) + "s"), target);
}
}
}
「バイト数」「文字数」どっちのパターンも試してみたけど、駄目そうね...

⇧ 0埋めじゃなくて、スペース埋めだけど...
「全角カナ」「半角カナ」でバイト数とか噛み合わない(涙)。
Java で半角カナ、全角カナとか文字数を意識しない、つまりバイト数で
ということで、
⇧ 先人の知恵を拝借。
OracleのSQLでも、正規表現っぽいことができるらしい。
結論としては、マトリックスを作っとくみたい。
だけど、はじめからbyteで考えていける環境であれば、バイト数で計算すれば良いみたい?です。 「全角カナ」「半角カナ」を意識する必要はないかと...たぶん。
それに、「全角カナ」「半角カナ」が混合した文字列とかも考え出すと、はじめからbyteで考えたほうが良さ気?ですかね。
package main;
public class TestKana {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
int strByteSize = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑをん".getBytes().length;
int strLenSize = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわゐゑをん".length();
String zenkakuKana = "レイワ、チョベリバ~!ピーパッパラッポ、アイムスキャットマ~ン!ガビーン";
String hankakuKana = "レイワ、チョベリバ~!ピーパッパラッポ、アイムスキャットマ~ン!ガビーン";
// boolean isByte = true;
// System.out.println("バイト数: " + strByteSize);
// System.out.println(zenkakuKana.getBytes().length);
// System.out.println(hankakuKana.getBytes().length);
// System.out.println(lpadding(zenkakuKana, strByteSize, isByte));
// System.out.println(lpadding(hankakuKana, strByteSize, isByte));
// System.out.println(lpadding(zenkakuKana, strByteSize, isByte).length());
// System.out.println(lpadding(hankakuKana, strByteSize, isByte).length());
// System.out.println(lpadding(zenkakuKana, strByteSize, isByte).getBytes().length);
// System.out.println(lpadding(hankakuKana, strByteSize, isByte).getBytes().length);
//
// isByte = false;
// System.out.println("文字数: " + strLenSize);
// System.out.println(zenkakuKana.length());
// System.out.println(hankakuKana.length());
// System.out.println(lpadding(zenkakuKana, strLenSize, isByte));
// System.out.println(lpadding(hankakuKana, strLenSize, isByte));
// System.out.println(lpadding(zenkakuKana, strLenSize, isByte).length());
// System.out.println(lpadding(hankakuKana, strLenSize, isByte).length());
// System.out.println(lpadding(zenkakuKana, strLenSize, isByte).getBytes().length);
// System.out.println(lpadding(hankakuKana, strLenSize, isByte).getBytes().length);
// 一旦、「半角カナ」を「全角カナ」に変換
String tmpHankakuKana = HanToZenForKKana(hankakuKana);
int diffByte = Math.abs(tmpHankakuKana.length() - hankakuKana.length());
System.out.println(diffByte);
System.out.println("全角: " + tmpHankakuKana.length());
System.out.println("半角: " + hankakuKana.length());
System.out.println("全角: " + tmpHankakuKana);
System.out.println("半角: " + hankakuKana);
System.out.println("全角: " + tmpHankakuKana.getBytes().length);
System.out.println("半角: " + hankakuKana.getBytes().length);
System.out.println(padding(tmpHankakuKana, strByteSize));
System.out.println(padding(hankakuKana, strByteSize));
System.out.println(padding(tmpHankakuKana, strByteSize).getBytes().length);
System.out.println(padding(hankakuKana, strByteSize).getBytes().length);
System.out.println("000000000000000000000".getBytes().length);
System.out.println("000000000".getBytes().length);
}
public static String padding(String target, int size) {
int end = target.getBytes().length;
for (int i = 0; i < size - end; i++) {
target = "0" + target;
}
return target;
}
public static String lpadding(String target, int size, boolean flg) {
// return String.format(("%"+ (size - target.getBytes().length) + "s"), target);
// return String.format(("%"+ (size - target.length()) + "s"), target);
if (flg) {
return String.format(("%" + (size - target.getBytes().length) + "s"), target);
} else {
return String.format(("%" + (size - target.length()) + "s"), target);
}
}
private static final String kanaHanZenTbl[][] = {
// 2文字構成の濁点付き半角カナ
// 必ずテーブルに先頭に置いてサーチ順を優先すること
{ "ガ", "ガ" }, { "ギ", "ギ" }, { "グ", "グ" }, { "ゲ", "ゲ" }, { "ゴ", "ゴ" },
{ "ザ", "ザ" }, { "ジ", "ジ" }, { "ズ", "ズ" }, { "ゼ", "ゼ" }, { "ゾ", "ゾ" },
{ "ダ", "ダ" }, { "ヂ", "ヂ" }, { "ヅ", "ヅ" }, { "デ", "デ" }, { "ド", "ド" },
{ "バ", "バ" }, { "ビ", "ビ" }, { "ブ", "ブ" }, { "ベ", "ベ" }, { "ボ", "ボ" },
{ "パ", "パ" }, { "ピ", "ピ" }, { "プ", "プ" }, { "ペ", "ペ" }, { "ポ", "ポ" },
{ "ヴ", "ヴ" },
// 1文字構成の半角カナ
{ "ア", "ア" }, { "イ", "イ" }, { "ウ", "ウ" }, { "エ", "エ" }, { "オ", "オ" },
{ "カ", "カ" }, { "キ", "キ" }, { "ク", "ク" }, { "ケ", "ケ" }, { "コ", "コ" },
{ "サ", "サ" }, { "シ", "シ" }, { "ス", "ス" }, { "セ", "セ" }, { "ソ", "ソ" },
{ "タ", "タ" }, { "チ", "チ" }, { "ツ", "ツ" }, { "テ", "テ" }, { "ト", "ト" },
{ "ナ", "ナ" }, { "ニ", "ニ" }, { "ヌ", "ヌ" }, { "ネ", "ネ" }, { "ノ", "ノ" },
{ "ハ", "ハ" }, { "ヒ", "ヒ" }, { "フ", "フ" }, { "ヘ", "ヘ" }, { "ホ", "ホ" },
{ "マ", "マ" }, { "ミ", "ミ" }, { "ム", "ム" }, { "メ", "メ" }, { "モ", "モ" },
{ "ヤ", "ヤ" }, { "ユ", "ユ" }, { "ヨ", "ヨ" },
{ "ラ", "ラ" }, { "リ", "リ" }, { "ル", "ル" }, { "レ", "レ" }, { "ロ", "ロ" },
{ "ワ", "ワ" }, { "ヲ", "ヲ" }, { "ン", "ン" },
{ "ァ", "ァ" }, { "ィ", "ィ" }, { "ゥ", "ゥ" }, { "ェ", "ェ" }, { "ォ", "ォ" },
{ "ャ", "ャ" }, { "ュ", "ュ" }, { "ョ", "ョ" }, { "ッ", "ッ" },
{ "。", "。" }, { "「", "「" }, { "」", "」" }, { "、", "、" }, { "・", "・" },
{ "ー", "ー" }, { "", "" }
};
public static String HanToZenForKKana(String p) {
StringBuffer sb = new StringBuffer();
for (int i = 0, j = 0; i < p.length(); i++) {
Character c = new Character(p.charAt(i));
// Unicode半角カタカナのコード範囲か?
if (c.compareTo(new Character((char) 0xff61)) >= 0
&& c.compareTo(new Character((char) 0xff9f)) <= 0) {
// 半角全角変換テーブルを検索する
for (j = 0; j < kanaHanZenTbl.length; j++) {
if (p.substring(i).startsWith(kanaHanZenTbl[j][0])) {
sb.append(kanaHanZenTbl[j][1]);
i += kanaHanZenTbl[j][0].length() - 1;
break;
}
}
// 検索できなければ、変換しない
if (j >= kanaHanZenTbl.length) {
sb.append(c);
}
} else { // Unicode半角カタカナ以外なら変換しない
sb.append(c);
}
}
return sb.toString();
}
}
結果

日本語って本当にプログラミングでは困った存在ですかね...
「文字数」でいくか、「バイト数」でいくか、それが問題だ、ですかね...
今回はこのへんで。