public class hiBytes extends Object
byte配列用のユーティリティです。
配列の指定位置にintを指定バイト長でセットしたり、逆に配列の指定位置に配置された指定長のintを取得したりできます。
byte[] _buf= new byte[128]; hiBytes.putInt1(0x61 ,_buf,0); // _buf[0]=0x61 hiBytes.putInt2(0x0A12 ,_buf,2); // _buf[2]=0x0A,_bud[3]=0x12 hiBytes.putInt4(0xF0115B23,_buf,5); // _buf[5]=0xF0,_bud[6]=0x11 // _buf[7]=0x5B,_bud[8]=0x23 // int _i1= hiBytes.getInt1(_buf,0); // _buf[0]を1byteの正のintにする int _i2= hiBytes.getInt2(_buf,2); // _buf[2],_buf[3]を2byteの正のintにする int _i3= hiBytes.getInt4(_buf,5); // _buf[5~8]を4byteのintにする
Javaのbyteはunsignedではないため、実際の用途にはほぼ使えません。
単に小さくデータをパックする、あるいは通信用にパックするといった用途としてのみbyteの配列が存在するとしています。
int _len = _buf[5]; // 0x80以上だとマイナスになる(良くあるバグ) int _len = hiBytes.getInt1(_buf,5);// _buf[5]&0xFFでもよいが常にhiBytesを使う // ことによりバグを防げる
byte配列の結合なども用意してあります。
byte[] _buf2= hiBytes.copyOf(_buf); // Arrays.copyOf(_buf,_buf.length)と同等 byte[] _buf3= hiBytes.concat(_buf,_buf2); // 連結(n個可能)
バイナリファイルを読み、文字列、数値を得ることができます。
BufferdInputStream bis = hiFile.openBinaryFileR("data.dat"); String name= hiBytes.readText(bis,4); // ascii4文字 int len = hiBytes.readInt(bis,4); // 整数4バイト hiBytes.skip(bis,32); // 32バイト読み飛ばす byte[] buf= hiBytes.readBytes(bis,len);
コンストラクタと説明 |
---|
hiBytes() |
修飾子とタイプ | メソッドと説明 |
---|---|
static boolean |
bitOff(byte[] data_,
int bitIndex_)
byte配列の指定bitをoffにする.
|
static boolean |
bitOff(byte[] data_,
int byteIndex_,
int bitIndex_)
byte配列の指定bitをoffにする.
|
static boolean |
bitOn(byte[] data_,
int bitIndex_)
byte配列の指定bitをonにする.
|
static boolean |
bitOn(byte[] data_,
int byteIndex_,
int bitIndex_)
byte配列の指定bitをonにする.
|
static String |
bits_part_str(byte[] buf_,
int bit_offset_,
int bits_len_)
byte配列中のビット並びを2進文字列で表す.
|
static String |
bits_str_1(int val_)
1バイト分のbit文字列を得る.
|
static String |
bits_str_2(int val_)
2バイト分のbit文字列を得る.
|
static String |
bits_str_4(int val_)
4バイト分のbit文字列を得る.
|
static String |
bits_str(byte[] buf_)
byte配列を2進文字列で表す.
|
static String |
bits_str(byte[] buf_,
int bytes_len_)
byte配列を指定長の2進文字列で表す.
|
static String |
bits_str(byte[] buf_,
int byte_offset_,
int bytes_len_)
byte配列中のビット並びを2進文字列で表す(バイト単位).
|
static String |
bits_str(int v_)
int値を2進数文字列で表す(負号無し).
int値を符号無し2進数文字列にします。
|
static String |
bits_str(int val_,
int len_)
bit文字列を得る.
|
static byte[] |
bytes(int data_)
intをbyteの配列に変換する.
|
static byte[] |
bytes(int[] data_)
intの配列をbyteの配列(バッファ)に変換する.
|
static byte[] |
concat(byte[]... values_)
複数のバイト配列内 容を連結した新たな配列を得る.
|
static byte[] |
copyOf(byte[] org_)
byte配列の複製を作る.
|
static byte[] |
copyOf(byte[] org_,
int offset_,
int length_)
byte配列の複製を作る.
|
static hiRef.V2<byte[],Integer> |
getBinBytes(String bitStr_,
boolean left_)
ビット文字列からバイト配列を得る。
|
static String |
getBitStr(byte[] buf_,
int offset_,
int len_)
byte配列を2進文字列にするにする.
|
static String |
getBitStr1(byte[] buf_,
int offset_)
byte配列中の1バイトを2進文字列にする.
|
static String |
getBitStr2(byte[] buf_,
int offset_)
byte配列の2バイトを2進文字列にする.
|
static String |
getBitStr4(byte[] buf_,
int offset_)
byte配列の4バイトを2進文字列にする.
|
static String |
getDecStr(byte[] buf_,
int offset_,
int len_)
byte配列内の指定範囲を10進文字列にするにする.
|
static String |
getDecStr1(byte[] buf_,
int offset_)
byte配列中の1バイトを10進文字列にする.
|
static String |
getDecStr2(byte[] buf_,
int offset_)
byte配列の2バイトを10進文字列にする.
|
static String |
getDecStr4(byte[] buf_,
int offset_)
byte配列の4バイトを10進文字列にする.
|
static double |
getDouble8(byte[] buf_,
int offset_)
8バイトを読みdoubleを得る.
|
static float |
getFloat4(byte[] buf_,
int offset_)
4バイトを読みfloatを得る.
|
static String |
getHexStr(byte[] buf_,
int offset_,
int len_)
byte配列を16進数にする.
|
static String |
getHexStr1(byte[] buf_,
int offset_)
byte配列中の1バイトを16進文字列にする.
|
static String |
getHexStr2(byte[] buf_,
int offset_)
byte配列中の2バイトを16進文字列にする.
|
static String |
getHexStr4(byte[] buf_,
int offset_)
byte配列中の4バイトを16進文字列にする.
|
static int |
getInt(byte[] buf_,
int offset_,
int size_)
指定バイト数を読みintを得る.
|
static int |
getInt1(byte[] buf_,
int offset_)
1バイトを読みintを得る.
|
static int |
getInt2(byte[] buf_,
int offset_)
2バイトを読みintを得る.
|
static int |
getInt3(byte[] buf_,
int offset_)
3バイトを読みintを得る.
|
static int |
getInt4(byte[] buf_,
int offset_)
4バイトを読みintを得る.
|
static int[] |
getInts(byte[] buf_)
バイト配列からint配列を得る(1単位4バイト).
|
static String |
getIntStr(byte[] buf_,
int offset_,
int len_)
byte配列内の指定範囲を10進文字列にするにする.
|
static String |
getIntStr1(byte[] buf_,
int offset_)
byte配列中の1バイトを10進文字列にする.
|
static String |
getIntStr2(byte[] buf_,
int offset_)
byte配列の2バイトを10進文字列にする.
|
static String |
getIntStr4(byte[] buf_,
int offset_)
byte配列の4バイトを10進文字列にする.
|
static short[] |
getShorts(byte[] buf_)
バイト配列からshort配列を得る(1単位2バイト)
愚かなJavaにはunsignedがないことに注意が必要
|
static String |
getUtf8Str(byte[] buf_,
int offset_,
int len_)
utf-8の文字列を得る.
|
static String |
hexStr(byte[] buf_)
byte配列内の指定範囲を16進数にする.
|
static int |
indexOf(int target_,
byte[] buf_,
int offset_,
int len_)
指定バイトを探す.
|
static int[] |
ints(byte[] data_)
byteの配列(バッファ)をintの配列に変換する.
|
static byte[] |
newBytes(byte[] buf_,
int offset_,
int len_)
byte配列の部分複製.
|
static byte[] |
newBytes(int iVal_,
int size_)
指定値を持つ指定長ののbyte配列を得る.
|
static byte[] |
newBytes1(int iVal_)
指定値を持つ長さ1のbyte配列を得る.
hiBytes.newBytes(x,1)と同じです。
|
static byte[] |
newBytes1fromInt(int iVal_)
指定値を持つ長さ1のbyte配列を得る.
hiBytes.newBytes(x,1)と同じです。
|
static byte[] |
newBytes2(int iVal_)
指定値を持つ長さ2のbyte配列を得る.
hiBytes.newBytes(x,2)と同じです。
|
static byte[] |
newBytes2fromInt(int iVal_)
指定値を持つ長さ2のbyte配列を得る.
hiBytes.newBytes(x,2)と同じです。
|
static byte[] |
newBytes3(int iVal_)
指定値を持つ長さ3のbyte配列を得る.
hiBytes.newBytes(x,3)と同じです。
|
static byte[] |
newBytes3fromInt(int iVal_)
指定値を持つ長さ3のbyte配列を得る.
hiBytes.newBytes(x,3)と同じです。
|
static byte[] |
newBytes4(float fVal_)
指定float値を持つ長さ4のbyte配列を得る.
|
static byte[] |
newBytes4(int iVal_)
指定値を持つ長さ4のbyte配列を得る.
hiBytes.newBytes(x,4)と同じです。
|
static byte[] |
newBytes4fromFloat(float fVal_)
指定float値を持つ長さ4のbyte配列を得る.
|
static byte[] |
newBytes4fromInt(int iVal_)
指定値を持つ長さ4のbyte配列を得る.
hiBytes.newBytes(x,4)と同じです。
|
static byte[] |
newBytes8(double dVal_)
指定float値を持つ長さ4のbyte配列を得る.
|
static byte[] |
newBytes8fromDouble(double dVal_)
指定float値を持つ長さ4のbyte配列を得る.
|
static byte[] |
newBytesFromInt(int iVal_,
int size_)
指定値を持つ指定長ののbyte配列を得る.
|
static byte[] |
parseBytes(String text_)
バイト並び(16進文字列)をパーズする
空白を含む16進文字列をパーズします。
|
static int |
putBytes(byte[] val_,
byte[] buf_,
int offset_)
バイト配列をバイト配列内部に上書きする.
|
static int |
putBytes(byte[] val_,
byte[] buf_,
int offset_,
int len_)
バイト配列をバイト配列内部に上書きする.
|
static byte[] |
putInt(int iVal_,
byte[] buf_,
int offset_,
int size_)
intを指定バイト数の領域にセットする.
|
static int |
putInt1(int iVal_,
byte[] buf_,
int offset_)
intを1バイトの領域にセットする.
|
static int |
putInt2(int iVal_,
byte[] buf_,
int offset_)
intを2バイトの領域にセットする.
|
static int |
putInt3(int iVal_,
byte[] buf_,
int offset_)
intを3バイトの領域にセットする
値が3バイトに収まらない場合は下位がセットされます。
|
static int |
putInt4(int iVal_,
byte[] buf_,
int offset_)
intを4バイトの領域にセットする
値が4バイトに収まらない場合は下位がセットされます。
|
static int |
putInts(int[] data_,
byte[] buf_)
intの配列をbyteの配列にセットする
int配列をbyte配列にセットします。
|
static int |
putInts(int[] data_,
byte[] buf_,
int offset_)
intの配列をbyteの配列にセットする
int配列をbyte配列にセットします。
|
static int |
putInts(int[] data_,
byte[] buf_,
int offset_,
int len_)
intの配列をbyteの配列にセットする
int配列をbyte配列にセットします。
|
static int |
putUtf8Str(String text_,
byte[] buf_,
int offset_,
int len_)
文字列をutf-8のbyte列に変換しbyte配列上にセットする.
|
static byte |
readByte(InputStream is_)
バイナリファイルからbyte(1バイト)を読む
|
static byte[] |
readBytes(InputStream is_,
int len_)
バイナリファイルからバイト配列を読む
|
static int |
readInt(InputStream is_)
バイナリファイルから整数(4bytes)を読む.
|
static int |
readInt(InputStream is_,
int len_)
バイナリファイルから整数を読む
|
static int[] |
readInts(InputStream is_,
int len_)
バイナリファイルからint配列(1要素:4bytes)を読む.
|
static short |
readShort(InputStream is_)
バイナリファイルからshort(2bytes)を読む
|
static short |
readShort(InputStream is_,
int len_)
バイナリファイルからshortを読む
|
static short[] |
readShorts(InputStream is_,
int len_)
バイナリファイルからshort配列(1要素2bytes)を読む.
|
static String |
readText(InputStream is_,
int len_)
バイナリファイルから文字列を読む
|
static String |
readText(InputStream is_,
int len_,
String charSet_)
バイナリファイルから文字列を読む
|
static byte[] |
resize(byte[] org_,
int new_len_)
バイト配列のlengthを変更した配列を得る.
|
static long |
skip(InputStream is_,
long len_)
バイナリファイルデータスキップ
|
static String |
str(byte[] buf_,
int offset_,
int len_,
String charSet_)
文字列を得る.
|
static byte[] |
str2bytes(String s_,
int len_)
文字列(charの並び)をbyte配列にする.
|
static byte[] |
toBytes(String hexStr_)
16進文字列からbyte配列を得る.
|
static byte[] |
toBytes(String hexStr_,
long option_)
16進文字列からbyte配列を得る.
|
public static final byte[] newBytes(int iVal_, int size_)
指定値を持つバイト配列を得ます。
値は下位から設定され、長さが満たない場合、上位が無視され、
長い場合0が埋められます。
byte _a[]= hiBytes.newBytes(0xABCDED,2); // -> [0xCD,0xEF] byte _b[]= hiBytes.newBytes(0xABCDEF,6); // -> [0x00,0x00,0x00,0xAB,0xCD,0xEF
iVal_
- 値size_
- バイト長public static final byte[] newBytes1(int iVal_)
iVal_
- 値public static final byte[] newBytes2(int iVal_)
iVal_
- 値public static final byte[] newBytes3(int iVal_)
iVal_
- 値public static final byte[] newBytes4(int iVal_)
iVal_
- 値public static final byte[] newBytes4(float fVal_)
fVal_
- 値public static final byte[] newBytes8(double dVal_)
dVal_
- 値public static final byte[] newBytesFromInt(int iVal_, int size_)
iVal_
- 値size_
- バイト長public static final byte[] newBytes1fromInt(int iVal_)
iVal_
- 値public static final byte[] newBytes2fromInt(int iVal_)
iVal_
- 値public static final byte[] newBytes3fromInt(int iVal_)
iVal_
- 値public static final byte[] newBytes4fromInt(int iVal_)
iVal_
- 値public static final byte[] newBytes4fromFloat(float fVal_)
fVal_
- 値public static final byte[] newBytes8fromDouble(double dVal_)
dVal_
- 値public static final byte[] putInt(int iVal_, byte[] buf_, int offset_, int size_)
intを指定バイト数の領域にセットします。4バイトより長い領域の場合、 若い番地のバイトには0x00(正の場合),0xFF(負の場合)がセットされます。
iVal_
- 値buf_
- 値を設定するbyte配列offset_
- 値の配置位置size_
- サイズpublic static final int putInt1(int iVal_, byte[] buf_, int offset_)
iVal_
- 値buf_
- 領域offset_
- オフセットpublic static final int putInt2(int iVal_, byte[] buf_, int offset_)
iVal_
- 値buf_
- 領域offset_
- オフセットpublic static final int putInt3(int iVal_, byte[] buf_, int offset_)
iVal_
- 値buf_
- 領域offset_
- オフセットpublic static final int putInt4(int iVal_, byte[] buf_, int offset_)
iVal_
- 値buf_
- 領域offset_
- オフセットpublic static final int putBytes(byte[] val_, byte[] buf_, int offset_)
次の操作と同様です。
int _min_len= Math.min(val_.length,buf_.length-offset_); System.arraycopy(val_,0,buf_,offset_,_len);
val_
- ソースbuf_
- 上書き先offset_
- 上書き先のオフセットpublic static final int putBytes(byte[] val_, byte[] buf_, int offset_, int len_)
次の操作と同様です。
int _min_len= Math.min(val_.length,len_); System.arraycopy(val_,0,buf_,offset_,_min_len);
val_
- ソースbuf_
- 上書き先offset_
- 上書き先のオフセットlen_
- 長さpublic static final int getInt(byte[] buf_, int offset_, int size_)
buf_
- 領域offset_
- オフセットsize_
- サイズpublic static final int getInt1(byte[] buf_, int offset_)
buf_
- 領域offset_
- オフセットpublic static final int getInt2(byte[] buf_, int offset_)
buf_
- 領域offset_
- オフセットpublic static final int getInt3(byte[] buf_, int offset_)
buf_
- 領域offset_
- オフセットpublic static final int getInt4(byte[] buf_, int offset_)
buf_
- 領域offset_
- オフセットpublic static final float getFloat4(byte[] buf_, int offset_)
buf_
- 領域offset_
- オフセットpublic static final double getDouble8(byte[] buf_, int offset_)
buf_
- 領域offset_
- オフセットpublic static final byte[] parseBytes(String text_)
空白を含む16進文字列をパーズします。空白は無視します。
空白あるいは16進文字以外の文字に出会うか文字列の最後に到達した時点でパーズは終了します。
3文字以上で文字数が奇数となる場合最後の文字は上位4bitと解釈されます。
text_
- 16進文字列public static final byte[] copyOf(byte[] org_)
次の操作と同等です。
byte[] _data= Arrays.copyOf(org_,org_.length);
org_
- 元の配列public static final byte[] resize(byte[] org_, int new_len_)
配列を指定サイズにします。
元々指定サイズだった場合はそのまま返ります。
元々のサイズと異なる場合は新たな配列が作成されます。
元々のサイズより指定サイズが小さい場合は後ろが捨てられます
元々のサイズより大きい場合は後ろの値は不定です。
org_
- 元の配列new_len_
- 新しい長さpublic static final byte[] concat(byte[]... values_)
values_
- 連結するバイト倍列public static final byte[] copyOf(byte[] org_, int offset_, int length_)
次の操作と同等です。
byte[] _data= Arrays.copyOfRange(org_,offset_,length_);
org_
- 元の配列offset_
- コピー元のオフセットlength_
- コピーする長さpublic static final int putInts(int[] data_, byte[] buf_, int offset_, int len_)
int配列をbyte配列にセットします。int一個あたり4byteとなります。
data_
- 変換するintの配列buf_
- バッファoffset_
- この位置(0オリジン)からセットするlen_
- セットする領域のサイズ(offset_+len_を超える場所にはセットしない)
-1ならbuf_の最後まで有効public static final int putInts(int[] data_, byte[] buf_)
int配列をbyte配列にセットします。int一個あたり4byteとなります。
data_
- 変換するintの配列buf_
- バッファpublic static final int putInts(int[] data_, byte[] buf_, int offset_)
int配列をbyte配列にセットします。int一個あたり4byteとなります。
data_
- 変換するintの配列buf_
- バッファoffset_
- この位置(0オリジン)からセットするpublic static final String hexStr(byte[] buf_)
hiHex.str(byte[])
buf_
- バイト並びpublic static final String getHexStr(byte[] buf_, int offset_, int len_)
hiHex.str(byte[])
参照buf_
- バイト並びoffset_
- オフセットlen_
- 長さpublic static final String getHexStr1(byte[] buf_, int offset_)
hiHex.str(byte[])
参照buf_
- バイト並びoffset_
- オフセットpublic static final String getHexStr2(byte[] buf_, int offset_)
hiHex.str(byte[])
参照buf_
- バイト並びoffset_
- オフセットpublic static final String getHexStr4(byte[] buf_, int offset_)
hiHex.str(byte[])
参照buf_
- バイト並びoffset_
- オフセットpublic static final String getDecStr(byte[] buf_, int offset_, int len_)
buf_
- バイト並びoffset_
- オフセットlen_
- 範囲長public static final String getDecStr1(byte[] buf_, int offset_)
buf_
- バイト並びoffset_
- オフセットpublic static final String getDecStr2(byte[] buf_, int offset_)
buf_
- バイト並びoffset_
- オフセットpublic static final String getDecStr4(byte[] buf_, int offset_)
buf_
- バイト並びoffset_
- オフセットpublic static final String getIntStr(byte[] buf_, int offset_, int len_)
buf_
- バイト並びoffset_
- オフセットlen_
- 長さpublic static final String getIntStr1(byte[] buf_, int offset_)
buf_
- バイト並びoffset_
- オフセットpublic static final String getIntStr2(byte[] buf_, int offset_)
buf_
- バイト並びoffset_
- オフセットpublic static final String getIntStr4(byte[] buf_, int offset_)
buf_
- バイト並びoffset_
- オフセットpublic static final String getBitStr(byte[] buf_, int offset_, int len_)
bits_str(byte[],int,int)
参照buf_
- バイト並びoffset_
- オフセットlen_
- 長さpublic static final String getBitStr1(byte[] buf_, int offset_)
bits_str(byte[],int,int)
参照buf_
- バイト並びoffset_
- オフセットpublic static final String getBitStr2(byte[] buf_, int offset_)
bits_str(byte[],int,int)
参照buf_
- バイト並びoffset_
- オフセットpublic static final String getBitStr4(byte[] buf_, int offset_)
bits_str(byte[],int,int)
参照buf_
- バイト並びoffset_
- オフセットpublic static final String getUtf8Str(byte[] buf_, int offset_, int len_)
byte配列内の指定範囲のデータをUTF-8として解釈します。
範囲内に0x00があればその前までとします。
buf_
- バイト並びoffset_
- オフセットlen_
- 長さpublic static final int putUtf8Str(String text_, byte[] buf_, int offset_, int len_)
文字列をutf-8のbyte列に変換しbyte配列上にセットします。 指定の長さより短い場合は0が埋められます。
text_
- 文字列buf_
- バイト並びoffset_
- オフセットlen_
- 長さpublic static final String str(byte[] buf_, int offset_, int len_, String charSet_)
byte配列内の指定範囲のデータを解釈し文字列にします。
文字列種は引数のcharSet_で選べます。
buf_
- byte配列offset_
- 開始位置len_
- 範囲長さcharSet_
- 文字列種public static final int indexOf(int target_, byte[] buf_, int offset_, int len_)
target_
- この値を探すbuf_
- この中を探すoffset_
- この位置から探すlen_
- この長さまでの位置を探すpublic static int[] getInts(byte[] buf_)
愚かなJavaにはunsignedがないことに注意が必要
buf_
- バイト配列(4バイト事に1int)public static short[] getShorts(byte[] buf_)
愚かなJavaにはunsignedがないことに注意が必要
buf_
- バイト配列(2バイト事に1short)public static String readText(InputStream is_, int len_)
is_
- 入力ストリームlen_
- バイト長hiException
- IOExceptionをcauseに持つpublic static String readText(InputStream is_, int len_, String charSet_)
is_
- 入力ストリームlen_
- バイト長charSet_
- 文字セットhiException
- IOExceptionをcauseに持つpublic static int readInt(InputStream is_)
is_
- 入力ストリームhiException
- IOExceptionをcauseに持つpublic static int readInt(InputStream is_, int len_)
is_
- 入力ストリームlen_
- バイト長hiException
- IOExceptionをcauseに持つpublic static short readShort(InputStream is_)
is_
- 入力ストリームhiException
- IOExceptionをcauseに持つpublic static short readShort(InputStream is_, int len_)
is_
- 入力ストリームlen_
- バイト長hiException
- IOExceptionをcauseに持つpublic static byte readByte(InputStream is_)
is_
- 入力ストリームhiException
- IOExceptionをcauseに持つpublic static byte[] readBytes(InputStream is_, int len_)
is_
- 入力ストリームlen_
- バイト長hiException
- IOExceptionをcauseに持つpublic static int[] readInts(InputStream is_, int len_)
is_
- 入力ストリームlen_
- バイト長hiException
- IOExceptionをcauseに持つpublic static short[] readShorts(InputStream is_, int len_)
is_
- 入力ストリームlen_
- バイト長hiException
- IOExceptionをcauseに持つpublic static long skip(InputStream is_, long len_)
is_
- 入力ストリームlen_
- バイト長hiException
- IOExceptionをcauseに持つpublic static byte[] bytes(int[] data_)
int配列をbyte配列に変換します。int一個あたり4byteとなります。
data_
- 変換するintの配列public static byte[] bytes(int data_)
int1個を4byteとしint配列をbyte配列に変換します。
data_
- intpublic static int[] ints(byte[] data_)
byteの配列の長さは4の倍数に足りない場合0が 補完され解釈されます。
data_
- 変換するbyteの配列public static boolean bitOn(byte[] data_, int bitIndex_)
極めて単純な計算式なのですが毎回考えるのは面倒なので
用意しました。コード上で意図を把握しやすくする意味もあります。
式は
data_[bitIndex_/8] |= (byte)(0x80>>(bitIndex_%8));
です。
data_
- バイト配列bitIndex_
- ビット位置(0オリジン)public static boolean bitOn(byte[] data_, int byteIndex_, int bitIndex_)
極めて単純な計算式なのですが毎回考えるのは面倒なので
用意しました。コード上で意図を把握しやすくする意味もあります。
式は
data_[byteIndex_] |= (byte)(0x80>>(bitIndex_));
です。
data_
- バイト配列byteIndex_
- バイト位置(0オリジン)bitIndex_
- ビット位置(0オリジン)public static boolean bitOff(byte[] data_, int bitIndex_)
極めて単純な計算式なのですが毎回考えるのは面倒なので
用意しました。コード上で意図を把握しやすくする意味もあります。
式は
data_[bitIndex_/8] &= (byte)(0xFFFF7FF>>(bitIndex_%8));
です。
data_
- バイト配列bitIndex_
- ビット位置(0オリジン)public static boolean bitOff(byte[] data_, int byteIndex_, int bitIndex_)
極めて単純な計算式なのですが毎回考えるのは面倒なので
用意しました。コード上で意図を把握しやすくする意味もあります。
式は
data_[byteIndex_] &= (byte)(0xFF7FF>>(bitIndex_));
です。
data_
- バイト配列byteIndex_
- バイト位置(0オリジン)bitIndex_
- ビット位置(0オリジン)public static hiRef.V2<byte[],Integer> getBinBytes(String bitStr_, boolean left_)
bitStr_
- ビット文字列。1,0以外は無視されますleft_
- 左詰指定。trueなら左詰め、falseなら右詰になりますpublic static byte[] newBytes(byte[] buf_, int offset_, int len_)
java.util.Arrays.copyRangeOf(buf,form,to)を使っても構いません。
buf_
- 元となるbyte配列offset_
- データ部オフセットlen_
- データ長public static String bits_str(int val_, int len_)
val_
- 値len_
- 取得するbit長public static String bits_str_1(int val_)
val_
- 値public static String bits_str_2(int val_)
val_
- 値public static String bits_str_4(int val_)
val_
- 値public static String bits_part_str(byte[] buf_, int bit_offset_, int bits_len_)
byte配列中の指定範囲bit並びを2進数文字列で表します。
byte[] data= {(byte)0x6E,(byte)0x93,(byte)0xC2,(byte)0x5B}; // 01101110 10010011 11000010 01011011 hiU.bits(data,0,18);// 01101110 10010011 11 (実際は空白なし) hiU.bits(data,1,18);// 1101110 10010011 110 (実際は空白なし) hiU.bits(data,17,3);// 100 hiU.bits(data,-1,6);// 001101 hiU.bits(data,29,5);// 011 00
bit_offset_がマイナスであったり、bits_len_が長くbuf_範囲を 外れるbitは"0"と表示されます。
buf_
- byte配列bit_offset_
- ビットオフセットbits_len_
- バイト長public static String bits_str(byte[] buf_, int byte_offset_, int bytes_len_)
byte配列中の指定範囲bit並びを2進数文字列で表します。
byte[] data= {(byte)0x6E,(byte)0x93,(byte)0xC2,(byte)0x5B}; // 01101110 10010011 11000010 01011011 hiU.bits(data,0,2);// 01101110 10010011 (実際は空白なし) hiU.bits(data,1,2);// 10010011 11000010 (実際は空白なし)
byte_offset_がマイナスであったり、bits_len_が長くbuf_範囲を 外れるbitは"0"と表示されます。
buf_
- byte配列byte_offset_
- バイト位置bytes_len_
- バイト長public static String bits_str(byte[] buf_)
buf_
- byte配列public static String bits_str(byte[] buf_, int bytes_len_)
buf_
- byte配列bytes_len_
- バイト長長public static String bits_str(int v_)
v_
- int値public static byte[] str2bytes(String s_, int len_)
1文字に2バイトがパックされた形で文字が並ぶ文字列から バイト配列を得ます。
String str= "ȃЅ؇ࠉฏ" +"∳䑕晷袙ꪻ쳝"; byte[] bytes=hiU.str2bytes(str,str.length()*2); // 次の値が得られる // {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07 // ,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f // ,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77 // ,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff}
s_
- 文字列len_
- バイト長(文字列長の2倍)public static byte[] toBytes(String hexStr_, long option_)
hiHex.toBytes(String,long)
と同じですhexStr_
- 16進文字列option_
- オプションpublic static byte[] toBytes(String hexStr_)
hiHex.toBytes(String)
と同じですhexStr_
- 16進文字列