byte配列とStringの相互回転

2235 ワード

/***byte[]配列をStringタイプ**@param arg変換が必要なbyte[]配列*@param length変換が必要な配列長*@return変換後のString隊形*/private String toHexString(byte[]arg,int length){String result=new String();if(arg!=null){for(int i=0;i/** * String byte[] * * @param arg String * @return byte[] */ private byte[] toByteArray(String arg) { if (arg != null) { /* 1. String ' ', String char */ char[] NewArray = new char[1000]; char[] array = arg.toCharArray(); int length = 0; for (int i = 0; i < array.length; i++) { if (array[i] != ' ') { NewArray[length] = array[i]; length++; } } /* char */ int EvenLength = (length % 2 == 0) ? length : length + 1; if (EvenLength != 0) { int[] data = new int[EvenLength]; data[EvenLength - 1] = 0; for (int i = 0; i < length; i++) { if (NewArray[i] >= '0' && NewArray[i] <= '9') { data[i] = NewArray[i] - '0'; } else if (NewArray[i] >= 'a' && NewArray[i] <= 'f') { data[i] = NewArray[i] - 'a' + 10; } else if (NewArray[i] >= 'A' && NewArray[i] <= 'F') { data[i] = NewArray[i] - 'A' + 10; } } /* char 16 */ byte[] byteArray = new byte[EvenLength / 2]; for (int i = 0; i < EvenLength / 2; i++) { byteArray[i] = (byte) (data[i * 2] * 16 + data[i * 2 + 1]); } return byteArray; } } return new byte[]{}; }