📄 toandfromhex.java
字号:
/*
Loader - tool for transfering data from one JDBC source to another and
doing transformations during copy.
Copyright (C) 2002-2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Loader.java
Date: 03.03.2003.
@version 2.1 alpha
@authors:
Radoslav Dutina rale@prozone.co.yu
*/
package org.webdocwf.util.loader;
import java.io.*;
/**
* ToAndFromHex class is used for transformation binary objects to string object, and
* string object to binary object
* @author Radoslav Dutina
* @version 1.0
*/
public class ToAndFromHex {
/**
* Empty constructor of ToAndFromHex class
*/
public ToAndFromHex() {
}
/**
* This method transform binary object to string object
* @param b is array of bytes which represents binary object
* @return string representation of binary object
*/
public static String getStringFromBlob(byte[] b){
if(b!=null){
ByteArrayInputStream is=new ByteArrayInputStream(b);
char[] hexBytes = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
int c;
String hexString=new String();
while ((c = is.read()) >= 0) {
hexString+=(hexBytes[(c >> 4) & 0xf]);
hexString+=(hexBytes[c & 0xf]);
}
return hexString;
}else{
return null;
}
}
/**
* This method transform string object to binary object (array of bytes)
* @param val is string representation of binary object
* @return binary object
*/
public static byte[] getByteArrayFromString(String val){
byte[] buf = new byte[val.length() / 2];
final char[] hexBytes = {
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
};
byte[] hexMap = new byte[256];
for (int i = 0; i < hexBytes.length; i++) {
hexMap[hexBytes[i]] = (byte)i;
}
int pos = 0;
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte)(hexMap[val.charAt(pos++)] << 4);
buf[i] += hexMap[val.charAt(pos++)];
}
return buf;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -