導航:首頁 > 編程語言 > 字元串轉16進制java

字元串轉16進制java

發布時間:2024-03-05 15:55:51

java字元串轉換為十六進制數組

字元串作為函數版change的參數權inputStr
byte[] change(String inputStr) {
byte[] result = new byte[inputStr.length() / 2];
for (int i = 0; i < inputStr.length() / 2; ++i)
result[i] = (byte)(Integer.parseInt(inputStr.substring(i * 2, i * 2 +2), 16) & 0xff);
return result;
}

㈡ 在線等!!!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編碼轉換
* 把16進制字元串轉換成位元組數組
* @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

㈢ Java 16進制字元串轉化成十六進制數字

沒懂啥意思,可以先將字元串轉化為整型,後面有需要了,再將整型轉回化為16進制的數答字

int parseInt = Integer.parseInt("cc", 16);

System.out.println(parseInt);

String hexString = Integer.toHexString(parseInt);

System.out.println(hexString);

㈣ java 字元串轉utf-8的十六進制

String str = URLEncoder.encode("字", "utf-8").replaceAll("%", "");

㈤ java 字元串轉十六進制,例如輸入010216,轉換後是0x01,0x020x10,就是每兩位的去轉請大俠指教

public static String cvtStr2Hex(String str) {
if (str == null)
return "";
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < str.length() / 2; i ++) {
String sub = str.substring(i*2, i*2 + 2);
sb.append("0x" + sub.toUpperCase() + " ");
}
// 轉換的時候一定注意將自己的文件編碼改成GBK
return sb.toString();
}

㈥ 怎麼把字元串轉化為十六進制字元串 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");

㈦ java中如何將byte[]裡面的數據轉換成十六進制

方法如下:

/* *

* Convert byte[] to hex string.這里我們可以將byte轉換成int,然後利用Integer.toHexString(int)

*來轉換成16進制字元串。

* @param src byte[] data

* @return hex string

*/

public static String bytesToHexString(byte[] src){

StringBuilder stringBuilder = new StringBuilder("");

if (src == null || src.length <= 0) {

return null;

}

for (int i = 0; i < src.length; i++) {

int v = src[i] & 0xFF;

String hv = Integer.toHexString(v);

if (hv.length() < 2) {

stringBuilder.append(0);

}

stringBuilder.append(hv);

}

return stringBuilder.toString();

}

此方法能將byte[]轉化成16進制字元串,

㈧ java中如何將字元串轉16位輸出、、。例如「aa」,"0000 0000 0000 0000"按這樣的方式輸出

先要以正確的編碼把字元串轉為位元組串,在把位元組串轉為16進制編碼
public class Test {
public static void main(String[] args) {
try{
System.out.println(toHex("hello world","GBK"));
}catch (UnsupportedEncodingException e){

e.printStackTrace();
}
}
static public String toHex(String text,String enc) throws UnsupportedEncodingException{
byte B[]=text.getBytes(enc);
StringBuilder buf=new StringBuilder();
for(byte b:B){
buf.append(Integer.toHexString(b&0xff));
}
return buf.toString();
}
}
==========
68656c6c6f20776f726c64

㈨ JAVA字元串轉16進制ascii碼

String s = "abcd";
byte[] b = s.getBytes();
int[] in = new int[b.length];
for (int i = 0; i < in.length; i++) {
in[i] = b[i]&0xff;
}
for (int j = 0; j < in.length; j++) {
System.out.println(Integer.toString(in[j], 0x10));
}

閱讀全文

與字元串轉16進制java相關的資料

熱點內容
手機唱吧找不到錄音文件 瀏覽:107
大數據時代財務管理的轉型措施 瀏覽:920
徐州網站開發有哪些 瀏覽:602
win10移動隱藏分區 瀏覽:617
文件清理有哪些可以刪 瀏覽:821
正方形怎麼數控編程 瀏覽:633
華為一鍵移機的文件在哪裡 瀏覽:386
心文件夾2神馬影院 瀏覽:645
當文件與ps不兼容 瀏覽:772
為什麼微信發不了nc格式的文件 瀏覽:134
九次方大數據公司咋樣 瀏覽:959
文件加密空間在哪裡 瀏覽:853
一個外國小孩被綁架的喜劇電影 瀏覽:555
食物鏈韓國中文版 瀏覽:412
姜銀慧演的電影 瀏覽:603
hik硬碟錄像機剪輯文件存哪裡 瀏覽:555
迷你編程怎麼弄玩法模式 瀏覽:111
小電影網址有哪些 瀏覽:193
b站會員怎麼升級lv3 瀏覽:827
編程時文件後綴怎麼設置 瀏覽:239

友情鏈接