@Test public void testStringToJISX0208() throws Exception { String s = "\u30000\uFF01\uFF71\u30A2\u3000"; byte[] expected = {0x23, 0x30, 0x21, 0x2a, 0x25, 0x22, 0x25, 0x22}; assertEquals(ByteUtil.stringToJISX0208(s), expected); }
public static byte[] stringToJISX0208(final String str) { if (str == null || str.length() <= 0) { return new byte[0]; } String s = str.replace('\t', ' ').trim(); StringBuilder buf = new StringBuilder(s); while (buf.charAt(0) == '\u3000') { buf.deleteCharAt(0); } int len = buf.length(); while (buf.charAt(len-1) == '\u3000') { buf.deleteCharAt(len-1); len = buf.length(); } byte[] b; try { b = buf.toString().getBytes("EUC-JP"); } catch (UnsupportedEncodingException e) { b = buf.toString().getBytes(Charset.defaultCharset()); } int size = b.length; if (size <= 0) { return new byte[0]; } byte[] tmp = new byte[size*2]; len = 0; for (int i=0; i<size; i++) { int high = b[i] & 0xff; int low; if (high >= 0x20 && high <= 0x7e) { int c = ASCII_TO_JISX0208_TABLE[high-0x20]; high = c >>> 8; low = c & 0xff; } else if (high >= 0xa1 && high <= 0xfe) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xfe) { high = high & 0x7f; low = low & 0x7f; } else { return new byte[0]; } i++; } else if (high == 0x8e) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xdf) { int c = JISX0201_TO_JISX0208_TABLE[low-0xa0]; high = c >>> 8; low = c & 0xff; } else { return new byte[0]; } i++; } else { return new byte[0]; } tmp[len] = (byte)high; tmp[len+1] = (byte)low; len += 2; } byte[] ret = new byte[len]; System.arraycopy(tmp, 0, ret, 0, len); return ret; }
ByteUtil { public static byte[] stringToJISX0208(final String str) { if (str == null || str.length() <= 0) { return new byte[0]; } String s = str.replace('\t', ' ').trim(); StringBuilder buf = new StringBuilder(s); while (buf.charAt(0) == '\u3000') { buf.deleteCharAt(0); } int len = buf.length(); while (buf.charAt(len-1) == '\u3000') { buf.deleteCharAt(len-1); len = buf.length(); } byte[] b; try { b = buf.toString().getBytes("EUC-JP"); } catch (UnsupportedEncodingException e) { b = buf.toString().getBytes(Charset.defaultCharset()); } int size = b.length; if (size <= 0) { return new byte[0]; } byte[] tmp = new byte[size*2]; len = 0; for (int i=0; i<size; i++) { int high = b[i] & 0xff; int low; if (high >= 0x20 && high <= 0x7e) { int c = ASCII_TO_JISX0208_TABLE[high-0x20]; high = c >>> 8; low = c & 0xff; } else if (high >= 0xa1 && high <= 0xfe) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xfe) { high = high & 0x7f; low = low & 0x7f; } else { return new byte[0]; } i++; } else if (high == 0x8e) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xdf) { int c = JISX0201_TO_JISX0208_TABLE[low-0xa0]; high = c >>> 8; low = c & 0xff; } else { return new byte[0]; } i++; } else { return new byte[0]; } tmp[len] = (byte)high; tmp[len+1] = (byte)low; len += 2; } byte[] ret = new byte[len]; System.arraycopy(tmp, 0, ret, 0, len); return ret; } }
ByteUtil { public static byte[] stringToJISX0208(final String str) { if (str == null || str.length() <= 0) { return new byte[0]; } String s = str.replace('\t', ' ').trim(); StringBuilder buf = new StringBuilder(s); while (buf.charAt(0) == '\u3000') { buf.deleteCharAt(0); } int len = buf.length(); while (buf.charAt(len-1) == '\u3000') { buf.deleteCharAt(len-1); len = buf.length(); } byte[] b; try { b = buf.toString().getBytes("EUC-JP"); } catch (UnsupportedEncodingException e) { b = buf.toString().getBytes(Charset.defaultCharset()); } int size = b.length; if (size <= 0) { return new byte[0]; } byte[] tmp = new byte[size*2]; len = 0; for (int i=0; i<size; i++) { int high = b[i] & 0xff; int low; if (high >= 0x20 && high <= 0x7e) { int c = ASCII_TO_JISX0208_TABLE[high-0x20]; high = c >>> 8; low = c & 0xff; } else if (high >= 0xa1 && high <= 0xfe) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xfe) { high = high & 0x7f; low = low & 0x7f; } else { return new byte[0]; } i++; } else if (high == 0x8e) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xdf) { int c = JISX0201_TO_JISX0208_TABLE[low-0xa0]; high = c >>> 8; low = c & 0xff; } else { return new byte[0]; } i++; } else { return new byte[0]; } tmp[len] = (byte)high; tmp[len+1] = (byte)low; len += 2; } byte[] ret = new byte[len]; System.arraycopy(tmp, 0, ret, 0, len); return ret; } private  ByteUtil(); }
ByteUtil { public static byte[] stringToJISX0208(final String str) { if (str == null || str.length() <= 0) { return new byte[0]; } String s = str.replace('\t', ' ').trim(); StringBuilder buf = new StringBuilder(s); while (buf.charAt(0) == '\u3000') { buf.deleteCharAt(0); } int len = buf.length(); while (buf.charAt(len-1) == '\u3000') { buf.deleteCharAt(len-1); len = buf.length(); } byte[] b; try { b = buf.toString().getBytes("EUC-JP"); } catch (UnsupportedEncodingException e) { b = buf.toString().getBytes(Charset.defaultCharset()); } int size = b.length; if (size <= 0) { return new byte[0]; } byte[] tmp = new byte[size*2]; len = 0; for (int i=0; i<size; i++) { int high = b[i] & 0xff; int low; if (high >= 0x20 && high <= 0x7e) { int c = ASCII_TO_JISX0208_TABLE[high-0x20]; high = c >>> 8; low = c & 0xff; } else if (high >= 0xa1 && high <= 0xfe) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xfe) { high = high & 0x7f; low = low & 0x7f; } else { return new byte[0]; } i++; } else if (high == 0x8e) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xdf) { int c = JISX0201_TO_JISX0208_TABLE[low-0xa0]; high = c >>> 8; low = c & 0xff; } else { return new byte[0]; } i++; } else { return new byte[0]; } tmp[len] = (byte)high; tmp[len+1] = (byte)low; len += 2; } byte[] ret = new byte[len]; System.arraycopy(tmp, 0, ret, 0, len); return ret; } private  ByteUtil(); static int getInt2(final byte[] b, final int offset); static int getInt3(final byte[] b, final int offset); static int getIntLE2(final byte[] b, final int offset); static int getBCD2(final byte[] b, final int offset); static int getBCD4(final byte[] b, final int offset); static long getLong4(final byte[] b, final int offset); static long getLong5(final byte[] b, final int offset); static long getLongLE4(final byte[] b, final int offset); static String narrowToWide(final String str); static String wideToNarrow(final String str); static int asciiToJISX0208(final int code); static int jisx0201ToJISX0208(final int code); static String gb2312ToString(final byte[] b); static String gb2312ToString(final byte[] b, final int offset, final int len); static String jisx0208ToString(final byte[] b); static String jisx0208ToString(final byte[] b, final int offset, final int len); static byte[] stringToJISX0208(final String str); static void katakanaToHiragana(final byte[] b); static void hiraganaToKatakana(final byte[] b); static void upperToLowerLatin(final byte[] b); static void lowerToUpperLatin(final byte[] b); static void upperToLower(final byte[] b); static void lowerToUpper(final byte[] b); static void convertLongVowel(final byte[] b); static void deleteLongVowel(final byte[] b); static void convertDoubleConsonant(final byte[] b); static void convertContractedSound(final byte[] b); static void convertVoicedConsonant(final byte[] b); static void convertSmallVowel(final byte[] b); static void convertPSound(final byte[] b); static void deleteMark(final byte[] b); static void deleteSpaceLatin(final byte[] b); static void deleteSpace(final byte[] b); static void reverseWordLatin(final byte[] b); static void reverseWord(final byte[] b); }
ByteUtil { public static byte[] stringToJISX0208(final String str) { if (str == null || str.length() <= 0) { return new byte[0]; } String s = str.replace('\t', ' ').trim(); StringBuilder buf = new StringBuilder(s); while (buf.charAt(0) == '\u3000') { buf.deleteCharAt(0); } int len = buf.length(); while (buf.charAt(len-1) == '\u3000') { buf.deleteCharAt(len-1); len = buf.length(); } byte[] b; try { b = buf.toString().getBytes("EUC-JP"); } catch (UnsupportedEncodingException e) { b = buf.toString().getBytes(Charset.defaultCharset()); } int size = b.length; if (size <= 0) { return new byte[0]; } byte[] tmp = new byte[size*2]; len = 0; for (int i=0; i<size; i++) { int high = b[i] & 0xff; int low; if (high >= 0x20 && high <= 0x7e) { int c = ASCII_TO_JISX0208_TABLE[high-0x20]; high = c >>> 8; low = c & 0xff; } else if (high >= 0xa1 && high <= 0xfe) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xfe) { high = high & 0x7f; low = low & 0x7f; } else { return new byte[0]; } i++; } else if (high == 0x8e) { low = b[i+1] & 0xff; if (low >= 0xa1 && low <= 0xdf) { int c = JISX0201_TO_JISX0208_TABLE[low-0xa0]; high = c >>> 8; low = c & 0xff; } else { return new byte[0]; } i++; } else { return new byte[0]; } tmp[len] = (byte)high; tmp[len+1] = (byte)low; len += 2; } byte[] ret = new byte[len]; System.arraycopy(tmp, 0, ret, 0, len); return ret; } private  ByteUtil(); static int getInt2(final byte[] b, final int offset); static int getInt3(final byte[] b, final int offset); static int getIntLE2(final byte[] b, final int offset); static int getBCD2(final byte[] b, final int offset); static int getBCD4(final byte[] b, final int offset); static long getLong4(final byte[] b, final int offset); static long getLong5(final byte[] b, final int offset); static long getLongLE4(final byte[] b, final int offset); static String narrowToWide(final String str); static String wideToNarrow(final String str); static int asciiToJISX0208(final int code); static int jisx0201ToJISX0208(final int code); static String gb2312ToString(final byte[] b); static String gb2312ToString(final byte[] b, final int offset, final int len); static String jisx0208ToString(final byte[] b); static String jisx0208ToString(final byte[] b, final int offset, final int len); static byte[] stringToJISX0208(final String str); static void katakanaToHiragana(final byte[] b); static void hiraganaToKatakana(final byte[] b); static void upperToLowerLatin(final byte[] b); static void lowerToUpperLatin(final byte[] b); static void upperToLower(final byte[] b); static void lowerToUpper(final byte[] b); static void convertLongVowel(final byte[] b); static void deleteLongVowel(final byte[] b); static void convertDoubleConsonant(final byte[] b); static void convertContractedSound(final byte[] b); static void convertVoicedConsonant(final byte[] b); static void convertSmallVowel(final byte[] b); static void convertPSound(final byte[] b); static void deleteMark(final byte[] b); static void deleteSpaceLatin(final byte[] b); static void deleteSpace(final byte[] b); static void reverseWordLatin(final byte[] b); static void reverseWord(final byte[] b);  }