java 字符串编码转bs64
时间: 2023-05-11 08:03:14 浏览: 90
可以使用Java自带的Base64类来进行字符串编码转换,示例代码如下:
```java
import java.util.Base64;
public class Main {
public static void main(String[] args) {
String str = "Hello, world!";
byte[] bytes = str.getBytes();
String encoded = Base64.getEncoder().encodeToString(bytes);
System.out.println(encoded);
}
}
```
输出结果为:
```
SGVsbG8sIHdvcmxkIQ==
```
这就是将字符串"Hello, world!"转换为Base64编码后的结果。
相关问题
java字符串转十六进制
Java中可以使用Integer类的toHexString()方法将字符串转换为十六进制字符串。具体实现方法如下所示:
```java
public static String stringToHexString(String s) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder();
byte[] bs = s.getBytes();
int bit;
for (byte b : bs) {
bit = (b & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = b & 0x0f;
sb.append(chars[bit]);
}
return sb.toString();
}
```
这个方法将输入的字符串s转换为字节数组bs,然后将每个字节转换为两位十六进制数,最后将所有的十六进制数拼接成一个字符串返回。
bs4.beautifulsoup 转字符串
可以使用BeautifulSoup对象的`prettify()`方法将其转换为字符串。例如:
```python
from bs4 import BeautifulSoup
html = '<html><head><title>Test</title></head><body><p>Hello, world!</p></body></html>'
soup = BeautifulSoup(html, 'html.parser')
string = soup.prettify()
print(string)
```
输出:
```
<html>
<head>
<title>
Test
</title>
</head>
<body>
<p>
Hello, world!
</p>
</body>
</html>
```