/** * 字符串转化成为16进制字符串 * @param * @return */
时间: 2023-07-06 21:41:06 浏览: 71
以下是Java中将字符串转换为16进制字符串的代码示例:
```
public static String stringToHex(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
sb.append(String.format("%02X ", (int) str.charAt(i)));
}
return sb.toString();
}
```
这个方法将给定的字符串转换为一个包含每个字符16进制表示的字符串。该方法使用StringBuilder来构建最终的16进制字符串,并使用String.format()将每个字符转换为两位16进制数。最后,返回StringBuilder中的字符串。
相关问题
实现一个字符串转16进制的函数模块
以下是Python语言实现的一个字符串转16进制的函数模块:
```python
def str_to_hex(string):
"""
将字符串转换为16进制表示
:param string: 字符串
:return: 16进制表示的字符串
"""
hex_string = ""
for s in string:
hex_string += hex(ord(s))[2:]
return hex_string
```
使用示例:
```python
print(str_to_hex("Hello, World!"))
# 输出: 48656c6c6f2c20576f726c6421
```
解释:
- `ord(s)` 函数返回字符的ASCII码值
- `hex()` 函数将10进制数字转换为16进制字符串,其中 `[2:]` 表示去掉字符串前面的 `0x` 前缀
JAVA spring boot 将长字符串生成12位唯一加密字符串,支持解密
可以使用Java提供的MessageDigest类来对字符串进行加密,生成唯一的加密字符串。具体实现方法如下:
1. 导入java.security.MessageDigest类
```java
import java.security.MessageDigest;
```
2. 创建一个MessageDigest对象
```java
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
```
3. 将需要加密的字符串转化为字节数组
```java
byte[] input = str.getBytes();
```
4. 使用MessageDigest对象进行加密,并将结果转化为字符串
```java
byte[] output = messageDigest.digest(input);
String result = bytesToHex(output);
```
其中,bytesToHex方法将字节数组转化为16进制字符串。
5. 截取生成的字符串前12位作为唯一加密字符串
```java
String encryptedStr = result.substring(0, 12);
```
6. 可以将加密后的字符串进行解密,但是MD5加密是不可逆的,无法进行解密。
完整代码如下:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class EncryptUtil {
/**
* 将字符串加密成唯一的12位字符串
*
* @param str 需要加密的字符串
* @return 唯一的加密字符串
*/
public static String encrypt(String str) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] input = str.getBytes();
byte[] output = messageDigest.digest(input);
String result = bytesToHex(output);
return result.substring(0, 12);
}
/**
* 将字节数组转化为16进制字符串
*
* @param bytes 字节数组
* @return 16进制字符串
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder stringBuilder = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
stringBuilder.append('0');
}
stringBuilder.append(hex);
}
return stringBuilder.toString();
}
}
```
使用方法:
```java
public static void main(String[] args) throws NoSuchAlgorithmException {
String str = "hello world";
String encryptedStr = EncryptUtil.encrypt(str);
System.out.println("加密前:" + str);
System.out.println("加密后:" + encryptedStr);
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)