Android 4.4(KitKat)でgetBytesの動作が変わった

424 views
Skip to first unread message

TechSearcher32

unread,
Dec 11, 2013, 8:04:58 PM12/11/13
to android-g...@googlegroups.com
お世話になります。


UnicodeからShif-JISに変換を行いたく、String#getBytes(String charsetName)を使用していました。
単体テストも作成していまして、Android 4.3 (Jelly Bean)までの環境では
正常にパスしていたのですが、Android 4.4 (KitKat)に環境を変更して
確認したところエラーになってしまいました。

どうやら4.4ではgetBytesで取得されるバイナリ配列の末尾1byteが欠けてしまっているようです。
  期待: 0x82, 0xA0
  結果: 0x82

この場合、どのような対処を行えばよいのでしょうか?

//参考ソース
public class TestSample extends AndroidTestCase {
    public void test_getBytes() throws UnsupportedEncodingException{
        String unicode = "あ";
        byte[] sjis = unicode.getBytes("Shift_JIS");
        byte[] expected = new byte[] {(byte)0x82, (byte)0xA0};
       
        assertEquals(2, sjis.length);              // 2byteであること
        assertTrue(Arrays.equals(expected, sjis)); // Shift_JISであること
    }
}
//以上

myamamic

unread,
Dec 11, 2013, 11:41:26 PM12/11/13
to android-g...@googlegroups.com
こんにちは

androidのissueリストに同じ問題と思われる報告がありました。
→4.4の不具合で、最新のコードでは修正されているみたいです。

対処法は、、、
上記報告で、getByte()に渡す文字が3文字以上だと正常との情報があるので、
末尾に何か余分な文字をつけるとかでしょうか。。。
かなり乱暴ですが。

2013年12月12日木曜日 10時04分58秒 UTC+9 TechSearcher32:
Reply all
Reply to author
Forward
0 new messages