16進制字元串轉16進制java
1. java把16進制的字元串裝換成16進制形式的byte數組
去掉0x以後轉整數再轉型成位元組
String[] s="0X0C 0X03 0X00 0X04 0X00 0X02 0X84 0XD7".replace("X", "x").split(" ");
byte[] b=new byte[s.length];
for(int i=0;i<s.length;i++){
b[i]=(byte)Integer.parseInt(s[i].substring(2),16);
}
System.out.println(Arrays.toString(b));
2. 怎麼把字元串轉化為十六進制字元串 java
思路:用一個初始化為0~9~a~f的字元串數組,也就是一個十六進制對應表,用這個對應表即可算出一個十六進制字元串的數值。
方法如下:
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f; //位於運算
sb.append(chars[bit]); //進行字元串的拼接
}
return sb.toString();
}
調用方法如下:
String str = str2HexStr("asbd");
3. Java 16進制字元串轉化成十六進制數字
沒懂啥意思,可以先將字元串轉化為整型,後面有需要了,再將整型轉化為專16進制的數字屬
intparseInt=Integer.parseInt("cc",16);
System.out.println(parseInt);
StringhexString=Integer.toHexString(parseInt);
System.out.println(hexString);
4. 二進制轉換成16進制 java string
public class Test {
public static void main(String[] args) {
String hex = "abef";
int i = Integer.parseInt(hex, 16);
System.out.println(hex);
String str = b2h(Integer.toBinaryString(i));
System.out.println(str);
}
static String[] hexStr = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "A", "B", "C", "D", "E", "F" };
public static String b2h(String binary) {
// 這里還可以做些判斷,比如傳進來的數字是否都是0和1
System.out.println(binary);
int length = binary.length();
int temp = length % 4;
// 每四位2進制數字對應一位16進制數字
// 補足4位
if (temp != 0) {
for (int i = 0; i < 4 - temp; i++) {
binary = "0" + binary;
}
}
// 重新計算長度
length = binary.length();
StringBuilder sb = new StringBuilder();
// 每4個二進制數為一組進行計算
for (int i = 0; i < length / 4; i++) {
int num = 0;
// 將4個二進制數轉成整數
for (int j = i * 4; j < i * 4 + 4; j++) {
num <<= 1;// 左移
num |= (binary.charAt(j) - '0');// 或運算
}
// 直接找到該整數對應的16進制,這里不用switch來做
sb.append(hexStr[num]);
// 這里如果要用switch case來做,大概是這個樣子
// switch(num){
// case 0:
// sb.append('0');
// break;
// case 1:
// ...
// case 15:
// sb.append('F');
// break;
// }
}
return sb.toString();
}
}
5. java字元串16進制怎麼轉換成數值
使用這個方法可以傳進去的16進制的數字組成的字元串轉化為utf-8格式的字元串
public static String toStringHex1(String s) {
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, "utf-8");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
6. 如何將16進制數字轉換成字元串 java
使用這個抄方法可襲以傳進去的16進制的數字組成的字元串轉化為utf-8格式的字元串
public
static
String
toStringHex1(String
s)
{
byte[]
baKeyword
=
new
byte[s.length()
/
2];
for
(int
i
=
0;
i
<
baKeyword.length;
i++)
{
try
{
baKeyword[i]
=
(byte)
(0xff
&
Integer.parseInt(s.substring(
i
*
2,
i
*
2
+
2),
16));
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
try
{
s
=
new
String(baKeyword,
"utf-8");//
UTF-16le:Not
}
catch
(Exception
e1)
{
e1.printStackTrace();
}
return
s;
}
7. java怎麼將16進制文件字元串轉成普通字元串
將指定byte數組以16進制的形式列印到控制台,代碼如下:
package com.nantian.iclient.atm.sdb;
public class Util {
public Util() {
}
/**
* 將指定byte數組以16進制的形式列印到控制台
* @param hint String
* @param b byte[]
* @return void
*/
public static void printHexString(String hint, byte[] b) {
System.out.print(hint);
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase() + " ");
}
System.out.println("");
}
/**
*
* @param b byte[]
* @return String
*/
public static String Bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
/**
* 將兩個ASCII字元合成一個位元組;
* 如:"EF"--> 0xEF
* @param src0 byte
* @param src1 byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte)(_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte)(_b0 ^ _b1);
return ret;
}
/**
* 將指定字元串src,以每兩個字元分割轉換為16進制形式
* 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9}
* @param src String
* @return byte[]
*/
public static byte[] HexString2Bytes(String src){
byte[] ret = new byte[8];
byte[] tmp = src.getBytes();
for(int i=0; i<8; i++){
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
}
return ret;
}
}
8. 在線等!!!java 如何將字元串轉換成十六進制
nt main(void)
4{
5 unsigned char array[4] = ;
6 unsigned long num;
7 num = 0;
8 for(int i=0; i<sizeof(array); i++)
9 {
10 num<<=8;
11 num |= array[i];
12 }
13 printf("num = %d",num);
14 return 0;
15
16}
二進制,位元組數組,字元,十六進制,BCD編碼轉換
* 把進制字元串轉換成位元組數組
* @param hex
* @return
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
public static final Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(in);
Object o = oi.readObject();
oi.close();
return o;
}
public static final byte[] objectToBytes(Serializable s) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream ot = new ObjectOutputStream(out);
ot.writeObject(s);
ot.flush();
ot.close();
return out.toByteArray();
}
public static final String objectToHexString(Serializable s) throws IOException{
return bytesToHexString(objectToBytes(s));
}
public static final Object hexStringToObject(String hex) throws IOException, ClassNotFoundException{
return bytesToObject(hexStringToByte(hex));
}
public static String bcd2Str(byte[] bytes){
StringBuffer temp=new StringBuffer(bytes.length*2);
for(int i=0;i<bytes.length;i++){
temp.append((byte)((bytes[i]& 0xf0)>>>4));
temp.append((byte)(bytes[i]& 0x0f));
}
return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();
}
public static byte[] str2Bcd(String asc) {
int len = asc.length();
int mod = len % 2;
if (mod != 0) {
asc = "0" + asc;
len = asc.length();
}
byte abt[] = new byte[len];
if (len >= 2) {
len = len / 2;
}
byte bbt[] = new byte[len];
abt = asc.getBytes();
int j, k;
for (int p = 0; p < asc.length()/2; p++) {
if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
j = abt[2 * p] - '0';
} else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
j = abt[2 * p] - 'a' + 0x0a;
} else {
j = abt[2 * p] - 'A' + 0x0a;
}
if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
k = abt[2 * p + 1] - '0';
} else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
k = abt[2 * p + 1] - 'a' + 0x0a;
}else {
k = abt[2 * p + 1] - 'A' + 0x0a;
}
int a = (j << 4) + k;
byte b = (byte) a;
bbt[p] = b;
}
return bbt;
}
public static String BCD2ASC(byte[] bytes) {
StringBuffer temp = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
int h = ((bytes[i] & 0xf0) >>> 4);
int l = (bytes[i] & 0x0f);
temp.append(BToA[h]).append( BToA[l]);
}
return temp.toString() ;
}
public static String MD5EncodeToHex(String origin) {
return bytesToHexString(MD5Encode(origin));
}
public static byte[] MD5Encode(String origin){
return MD5Encode(origin.getBytes());
}
public static byte[] MD5Encode(byte[] bytes){
MessageDigest md=null;
try {
md = MessageDigest.getInstance("MD5");
return md.digest(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return new byte[0];
}
}
//關於byte: signed byte 把 0x00 ~ 0xff 映射成范圍 0~127和 -128~-1 兩段,比較簡單的辦法用 (b+256)%256的辦法令其值回到0~255,或者用&0xff並賦給一個int
9. java字元串轉16進制
String str="鯨";
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++)
{
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
sb.append(' ');
}
System.out.println(sb.toString().trim());
這樣? 不過我怎麼轉都得不到你要說版的值。。權。
10. java怎麼把16進制的字元串轉化為十進制
toHexString
public static String toHexString(int
i)以十六進制的無符號整數形式返回一個整數參數的字元串表示形式。
如果參數為負,那麼無符號整數值為參數加上
232;否則等於該參數。將該值轉換為十六進制(基數 16)的無前導 0 的 ASCII 數字字元串。如果無符號數的大小值為零,則用一個零字元 '0'
('\u0030') 表示它;否則,無符號數大小的表示形式中的第一個字元將不是零字元。用以下字元作為十六進制數字:
0123456789abcdef
這些字元的范圍是從 '\u0030' 到 '\u0039' 和從 '\u0061' 到 '\u0066'。如果希望得到大寫字母,可以在結果上調用
String.toUpperCase() 方法:
Integer.toHexString(n).toUpperCase()
參數:
i
- 要轉換成字元串的整數。
返回:
用十六進制(基數 16)參數表示的無符號整數值的字元串表示形式。
// 轉化字元串為十六進制編碼
public static String toHexString(String s)
{
String str="";
for
(int i=0;i<s.length();i++)
{
int ch = (int)s.charAt(i);
String s4
= Integer.toHexString(ch);
str = str + s4;
}
return str;
}
// 轉化十六進制編碼為字元串
public static String toStringHex(String s)
{
byte[] baKeyword = new byte[s.length()/2];
for(int i = 0; i <
baKeyword.length; i++)
{
try
{
baKeyword[i] = (byte)(0xff &
Integer.parseInt(s.substring(i*2, i*2+2),16));
}
catch(Exception e)
{
e.printStackTrace();
}
}
try
{
s = new
String(baKeyword, "utf-8");//UTF-16le:Not
}
catch (Exception e1)
{