go byte 转string 去掉 转义字符
时间: 2023-11-21 12:06:48 浏览: 254
在 Go 中,可以使用 `strconv.Unquote` 函数将带有转义字符的字节序列转换为字符串,并去掉转义字符。示例代码如下:
```go
package main
import (
"fmt"
"strconv"
)
func main() {
b := []byte(`{"name":"John","age":30,"city":"New\nYork"}`)
s, err := strconv.Unquote(string(b))
if err != nil {
fmt.Println(err)
}
fmt.Println(s)
}
```
输出结果为:
```
{"name":"John","age":30,"city":"New
York"}
```
其中,`Unquote` 函数会从提供的字符串中解析并返回一个未转义的字符串。如果字符串不是一个带有引号的字符串字面值,则返回错误。
相关问题
java代码中对含有&的String字符串进行转义的代码
敏感信息的字符串进行加密处理的示例:
1. 使用Java的加密库进行加密处理:
```
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class EncryptUtil {
private static final String ENCRYPT_ALGORITHM = "AES";
private static final String ENCRYPT_KEY = "mysecretkey";
public static String encrypt(String input) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPT_ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
return new String(encryptedBytes);
}
public static String decrypt(String encryptedInput) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPT_ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedInput.getBytes());
return new String(decryptedBytes);
}
}
```
使用示例:
```
String input = "my sensitive information";
String encryptedInput = EncryptUtil.encrypt(input);
String decryptedInput = EncryptUtil.decrypt(encryptedInput);
System.out.println("Input: " + input);
System.out.println("Encrypted Input: " + encryptedInput);
System.out.println("Decrypted Input: " + decryptedInput);
```
2. 使用Base64编码进行加密处理:
```
import java.util.Base64;
public class EncryptUtil {
public static String encrypt(String input) {
byte[] encryptedBytes = Base64.getEncoder().encode(input.getBytes());
return new String(encryptedBytes);
}
public static String decrypt(String encryptedInput) {
byte[] decryptedBytes = Base64.getDecoder().decode(encryptedInput.getBytes());
return new String(decryptedBytes);
}
}
```
使用示例:
```
String input = "my sensitive information";
String encryptedInput = EncryptUtil.encrypt(input);
String decryptedInput = EncryptUtil.decrypt(encryptedInput);
System.out.println("Input: " + input);
System.out.println("Encrypted Input: " + encryptedInput);
System.out.println("Decrypted Input: " + decryptedInput);
```
在jemter中byte类型{0x03}怎么转换为字符串
您可以使用Java语言中的String类来将byte类型转换为字符串,具体的代码如下所示:
```
byte[] bytes = new byte[]{0x03};
String str = new String(bytes);
System.out.println(str); // 输出为字符串 "\u0003"
```
需要注意的是,由于0x03是一个控制字符,所以在输出字符串时,会显示为Unicode转义字符"\u0003",而不是直接显示为字符"3"。如果您需要将其转换为可见字符,可以使用转义字符"\t"来代替,具体的代码如下所示:
```
byte[] bytes = new byte[]{0x03};
String str = new String(bytes).replaceAll("\u0003", "\\t");
System.out.println(str); // 输出为字符串 "\t"
```
这样就可以将byte类型的数据转换为字符串,并输出为可见字符了。
阅读全文