16進数文字列(String)⇔バイト配列(byte[])

※2013/01/08注
なんか未だに参照されるようなので。。。
DatatypeConverterを使ってみてはどうでしょう?

    • -


わりとよく使うので。


public class HexUtil {

/**
* バイト配列を16進数の文字列に変換する。
*
* @param bytes バイト配列
* @return 16進数の文字列
*/
public static String asHex(byte bytes) {
// バイト配列の2倍の長さの文字列バッファを生成。
StringBuffer strbuf = new StringBuffer(bytes.length * 2);

// バイト配列の要素数分、処理を繰り返す。
for (int index = 0; index < bytes.length; index++) {
// バイト値を自然数に変換。
int bt = bytes[index] & 0xff;

// バイト値が0x10以下か判定。
if (bt < 0x10) {
// 0x10以下の場合、文字列バッファに0を追加。
strbuf.append("0");
}

// バイト値を16進数の文字列に変換して、文字列バッファに追加。
strbuf.append(Integer.toHexString(bt));
}

/// 16進数の文字列を返す。
return strbuf.toString();
}

/**
* 16進数の文字列をバイト配列に変換する。
*
* @param hex 16進数の文字列
* @return バイト配列
*/
public static byte asByteArray(String hex) {
// 文字列長の1/2の長さのバイト配列を生成。
byte bytes = new byte[hex.length() / 2];

// バイト配列の要素数分、処理を繰り返す。
for (int index = 0; index < bytes.length; index++) {
// 16進数文字列をバイトに変換して配列に格納。
bytes[index] =
(byte) Integer.parseInt(
hex.substring(index * 2, (index + 1) * 2),
16);
}

// バイト配列を返す。
return bytes;
}

public static void main(String args) {
byte bytes = {0x23, 0x0F};

for(int i=0; i bytes2 = asByteArray(hex);

for(int i=0; i
実行結果は以下の通り。


23 f
230f
23 f

Mapをロック

Beanのほうが安全だけど、Mapを使いたいな〜…というときのための一アイデア。
コンパイル時にはエラーをはじけないけど、動かせばすぐに分かると思う…たぶん。


import java.util.HashMap;

public class LockedMap extends HashMap {

private boolean lock = false;

public void lock() {
lock = true;
}

public void unlock() {
lock = false;
}

public Object get(Object key) {
if(lock && !this.containsKey(key)) {
throw new RuntimeException("キー[" + key + "]は存在しません。");
}

return super.get(key);
}

public Object put(Object key, Object value) {
if(lock) {
throw new RuntimeException("インスタンスはロックされています。");
}

return super.put(key, value);
}

public static void main(String[] args) {
LockedMap map = new LockedMap();

map.put("キー", "値");

System.out.println(map.get("キー"));
System.out.println(map.get("似非キー"));

map.lock();

System.out.println(map.get("キー"));
System.out.println(map.get("似非キー"));
}

}

実行結果は次の通り。


null

java.lang.RuntimeException: キー[似非キー]は存在しません。
at LockedMap.get(LockedMap.java:17)
at LockedMap.main(LockedMap.java:42)
Exception in thread "main"

join() または implode()

配列にjoin()メソッドがあるといいな…と思ったので。*1

Java版join() (配列)


public class Hoge {
public static void main(String args) throws Exception {
String
array1 = {"aaa", "bbb", "ccc"};
String array2 = {"111", null, "333", ""};
String
array3 = null;

System.out.println("array1: [" + join(array1, ", ") + "]");
System.out.println("array2: [" + join(array2, " AND ") + "]");
System.out.println("array3: [" + join(array3, " : ") + "]");
}

public static String join(Object[] array, String sep) {
if(array == null) {
return null;
} else if(array.length < 1) {
return "";
} else if(array.length < 2) {
return (array[0] != null) ? array[0].toString() : "";
}

StringBuffer buf = new StringBuffer((array[0] != null) ? array[0].toString() : "");

for(int i=1; i
実行結果は次の通り。


array1: [aaa, bbb, ccc]
array2: [111 AND AND 333 AND ]
array3: [null]

Java版join() (リスト)

インターフェースを拡張したほうがよいのかな…?


import java.util.Arrays;
import java.util.List;

public class Hoge2 {
public static void main(String args) throws Exception {
List list1 = Arrays.asList(new String
{"aaa", "bbb", "ccc"});
List list2 = Arrays.asList(new String[]{"111", null, "333", ""});
List list3 = null;

System.out.println("list1: [" + join(list1, ", ") + "]");
System.out.println("list2: [" + join(list2, " AND ") + "]");
System.out.println("list3: [" + join(list3, " : ") + "]");
}

public static String join(List list, String sep) {
if(list == null) {
return null;
} else if(list.size() < 1) {
return "";
} else if(list.size() < 2) {
return (list.get(0) != null) ? list.get(0).toString() : "";
}

StringBuffer buf = new StringBuffer((list.get(0) != null) ? list.get(0).toString() : "");

for(int i=1; i

*1:Commonsにあるかも