日期:2014-05-18  浏览次数:20748 次

字节数组怎么转换成二进制字符串
字节数组怎么转换成二进制字符串
或者如何将blob字段类型数据转换成二进制字符串

麻烦各位老大给出具体代码怎么写?

------解决方案--------------------
用Integer的toBinaryString将字节一个个的转换成字符串,注意之后要自己处理字符串的长度。因为toBinaryString产生的字符串可能不足8个字符长度,也可能是32个字符长度。
------解决方案--------------------
Blob blob = rs.getBlob(1);
/*把DB中文件的内容写入到磁盘文件*/
FileOutputStream fout = new FileOutputStream( "D:\\123.jpg ");
fout.write(blob.getBytes(1, (int)blob.length()));
fout.flush();
fout.close();
------解决方案--------------------
byte b = ...
int n = b;
if(n < 0)
n += 256;

String s1 = Integer.toHexString(n);//16进制
String s2 = Integer.toBinaryString(n);//二进制