代码如下所示:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
/*输入一个byte和byte[]合并为byte[]*/
public byte[] bytemerger(byte byte_1, byte[] byte_2) {
byte[] byte_3 = new byte[1 + byte_2.length];
byte_3[0] = byte_1;
system.arraycopy(byte_2, 0, byte_3, 1, byte_2.length);
return byte_3;
}
/*输入一个byte[]和byte[]合并为byte[]*/
public byte[] bytemerger(byte[] byte_1, byte[] byte_2) {
byte[] byte_3 = new byte[1 + byte_2.length];
byte_3[0] = byte_1;
system.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
return byte_3;
}
/*输入一个string(16进制的字符hex eg:ff)输出为16进制的byte[],注意输入为小写的hex字符串*/
public 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) (chartobyte(achar[pos]) << 4 | chartobyte(achar[pos + 1]));
}
//system.out.println(arrays.tostring(result));
return result;
}
private byte chartobyte(char c) {
//return (byte) "0123456789abcdef".indexof(c);
return (byte) "0123456789abcdef".indexof(c);
}
/*输入10进制数字字符串,输出hex字符串(2位,eg: f 则输出 0f)*/
string value= "100";
int parseint = integer.parseint(value, 10);
string hexstring = integer.tohexstring(parseint);
if (hexstring.length() < 2) {
hexstring = '0' + hexstring;
}
header = header + hexstring;
}
/*输入16进制byte[]输出16进制字符串*/
public static string bytearraytohexstr(byte[] bytearray) {
if (bytearray == null) {
return null;
}
char[] hexarray = "0123456789abcdef".tochararray();
char[] hexchars = new char[bytearray.length * 2];
for (int j = 0; j < bytearray.length; j++) {
int v = bytearray[j] & 0xff;
hexchars[j * 2] = hexarray[v >>> 4];
hexchars[j * 2 + 1] = hexarray[v & 0x0f];
}
return new string(hexchars);
}
|
ps:下面看下js对url中特殊字符的转换
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
let str = "http%3a%2f%2fxxxxxxxx%2findex.php%2fxxxxxxx%2fmember%2fregister%3frecommend_id%3d11442%26id%3d87";
function replacestr(str){
str = str.replace(/%3a/g, ":");
str = str.replace(/%2f/g, "/");
str = str.replace(/%3f/g, "?");
str = str.replace(/%3d/g, "=");
str = str.replace(/%26/g, "&");
str = str.replace(/%2b/g, "+");
str = str.replace(/%20/g, " ");
str = str.replace(/%23/g, "#");
return str;
}
console.log(replacestr(str));
|
总结
以上所述是小编给大家介绍的java中byte[]、string、hex字符串等转换的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
asp.net System.Guid ToString五种格式
2025-05-29 43 -
2025-05-27 65
-
2025-06-04 32
-
2025-05-29 50
-
2025-06-04 108
热门评论

