String str = new String(byte[] byteArray);
Convert a byte array to a Hex stringTag(s):
The simple way
public static String getHexString(byte[] b) throws Exception {
String result = "";
for (int i=0; i < b.length; i++) {
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
A faster way
import java.io.UnsupportedEncodingException;
public class StringUtils {
static final byte[] HEX_CHAR_TABLE = {
(byte)'0', (byte)'1', (byte)'2', (byte)'3',
(byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b',
(byte)'c', (byte)'d', (byte)'e', (byte)'f'
};
public static String getHexString(byte[] raw)
throws UnsupportedEncodingException
{
byte[] hex = new byte[2 * raw.length];
int index = 0;
for (byte b : raw) {
int v = b & 0xFF;
hex[index++] = HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF];
}
return new String(hex, "ASCII");
}
public static void main(String args[]) throws Exception{
byte[] byteArray = {
(byte)255, (byte)254, (byte)253,
(byte)252, (byte)251, (byte)250
};
System.out.println(StringUtils.getHexString(byteArray));
/*
* output :
* fffefdfcfbfa
*/
}
}
A more elegant (based on a suggestion by Lew on usenet-cljp)
static final String HEXES = "0123456789ABCDEF";
public static String getHex( byte [] raw ) {
if ( raw == null ) {
return null;
}
final StringBuilder hex = new StringBuilder( 2 * raw.length );
for ( final byte b : raw ) {
hex.append(HEXES.charAt((b & 0xF0) >> 4))
.append(HEXES.charAt((b & 0x0F)));
}
return hex.toString();
}
<!-- HOWTO content -->
分享到:
相关推荐
public static String bytes2Hex(byte[] data) { if (data==null) { return null; } else { int len = data.length; String str = ""; for (int i=0; i; i++) { if ((data[i]&0xFF)) str = str + "0" + ...
String2Byte(dataStr As String, [ChatSet As String]) As Array(Byte) getFileBytes(fileName) As Array(Byte) 3.8 文件拆分和合并 ---------------------------------------------------------------------------...
public static String toHexString(byte[] buf) { char[] chars = new char[2 * buf.length]; for (int i = 0; i ; ++i) { chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4]; chars[2 * i + 1] = HEX_CHARS...
'Convert the byte array to a string. DevicePathName = CStr(DetailDataBuffer()) 'Convert to Unicode. DevicePathName = StrConv(DevicePathName, vbUnicode) 'Strip cbSize (4 bytes) from the beginning....
Added a statusbar to the hexview in memoryview Pointerscan for value scans now add the results to the overflow queue Opening a file and changing bytes do not change them to the file anymore (you need ...
CByte CCur CDate CDbl Chr CInt CLng CSng CStr Hex Oct FormatCurrency FormatDateTime FormatNumber FormatPercent Abs Atn Cos Exp Hex Int Fix Log Oct Rnd Sgn Sin Sqr Tan Array Filter IsArray Join LBound ...
PEP 3101: A New Approach To String Formatting Changes To Exceptions Miscellaneous Other Changes Operators And Special Methods Builtins Build and C API Changes Performance Porting To Python 3.0 ...
* HEX(num):返回十六进制值 * OCT(num):返回八进制值 * SQR(num):返回平方根 * INT(num):取整 * FIX(num):取整 * ROUND(num, n):四舍五入取小数位 * LOG(num):取以 e 为底的对数 * EXP(n):取 e 的 n 次幂 * ...
Pointers to Structures 5.7 - Sample Programs 5.7.1 - Simple Variable Declarations 5.7.2 - Using Pointer Variables 5.7.3 - Single Dimension Array Access 5.7.4 - Multidimensional Array ...
String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...
String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...
String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...
String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...
String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...