@@ -93,24 +93,24 @@ public static byte[] toByteArray(char[] txt) {
9393 /**
9494 * 16進数をバイト列に変換する。
9595 *
96- * @param txt 2の倍数の長さ限定 16進数文字列
96+ * @param hex 2の倍数の長さ限定 16進数文字列
9797 * @param offset txt の16進数の位置
9898 * @param length txt の16進数の長さ
9999 * @return バイト列
100100 */
101- public static byte [] toByteArray (char [] txt , int offset , int length ) {
101+ public static byte [] toByteArray (char [] hex , int offset , int length ) {
102102 int bit = 4 ;
103103 byte [] data = new byte [(length * bit + 7 ) / 8 ];
104104 int bd = 0 ;
105105 int bitlen = 0 ;
106106 int j = 0 ;
107107 for (int i = 0 ; i < length ; i ++) {
108- int a = txt [offset + i ];
108+ int a = hex [offset + i ];
109109 if (a >= '0' && a <= '9' ) {
110110 a -= '0' ;
111- } else if (a >= 'a' && a <= 'z ' ) {
111+ } else if (a >= 'a' && a <= 'f ' ) {
112112 a -= 'a' - 10 ;
113- } else if (a >= 'A' && a <= 'Z ' ) {
113+ } else if (a >= 'A' && a <= 'F ' ) {
114114 a -= 'A' - 10 ;
115115 } else {
116116 throw new java .lang .IllegalStateException ();
@@ -195,6 +195,47 @@ public static byte[] toByteArray(BigInteger num, int length) {
195195 return data ;
196196 }
197197
198+ public static byte [] toByte (short i ) {
199+ byte [] out = new byte [2 ];
200+ out [0 ] = (byte ) (i >>> 8 );
201+ out [1 ] = (byte ) i ;
202+ return out ;
203+ }
204+
205+ public static byte [] toByte (short i , byte [] out , int offset ) {
206+ out [offset ++] = (byte ) (i >>> 8 );
207+ out [offset ] = (byte ) i ;
208+ return out ;
209+ }
210+
211+ public static byte [] toByte (int i ) {
212+ return toByte (i , new byte [4 ], 0 );
213+ }
214+
215+ public static byte [] toByte (int i , byte [] out , int offset ) {
216+ out [offset ++] = (byte ) (i >>> 24 );
217+ out [offset ++] = (byte ) (i >> 16 );
218+ out [offset ++] = (byte ) (i >>> 8 );
219+ out [offset ] = (byte ) i ;
220+ return out ;
221+ }
222+
223+ public static byte [] toByte (long l ) {
224+ byte [] out = new byte [8 ];
225+ return toByte (l , out , 0 );
226+ }
227+ public static byte [] toByte (long l , byte [] out , int offset ) {
228+ out [0 ] = (byte ) (l >>> 56 );
229+ out [1 ] = (byte ) (l >> 48 );
230+ out [2 ] = (byte ) (l >> 40 );
231+ out [3 ] = (byte ) (l >> 32 );
232+ out [4 ] = (byte ) (l >> 24 );
233+ out [5 ] = (byte ) (l >> 16 );
234+ out [6 ] = (byte ) (l >>> 8 );
235+ out [7 ] = (byte ) l ;
236+ return out ;
237+ }
238+
198239 // Bin Byte系機能
199240
200241 /**
@@ -259,4 +300,20 @@ public static byte[] xor(byte[] a, byte[] b) {
259300 }
260301 return ret ;
261302 }
303+
304+ /**
305+ * MSB が 配列 0 側にある想定のシフト演算
306+ * @param a 配列
307+ * @param shift シフトビット数 とりあえず 0 から 7
308+ * @return シフトされた列
309+ */
310+ public static byte [] left (byte [] a , int shift ) {
311+ byte [] b = new byte [a .length ];
312+ for ( int i = 0 ; i < a .length - 1 ; i ++ ) {
313+ b [i ] = (byte )((a [i ] & 0xff ) << shift | ((a [i +1 ] & 0xff ) >> (shift - 8 )));
314+ }
315+ b [a .length - 1 ] = (byte )((a [a .length - 1 ] & 0xff ) << shift );
316+ return b ;
317+ }
318+
262319}
0 commit comments