Skip to content

Commit 93a89b7

Browse files
committed
ascii/unicode to/from string conversion snippets
1 parent 9eb5875 commit 93a89b7

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Ascii To String
3+
description: Converts a list of ascii numbers into a string
4+
author: Mcbencrafter
5+
tags: string,ascii,encoding,decode,conversion
6+
---
7+
8+
```java
9+
import java.util.List;
10+
11+
public static String asciiToString(List<Integer> asciiCodes) {
12+
StringBuilder text = new StringBuilder();
13+
14+
for (int asciiCode : asciiCodes) {
15+
text.append((char) asciiCode);
16+
}
17+
18+
return text.toString();
19+
}
20+
21+
// Usage:
22+
System.out.println(asciiToString(List.of(104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100))); // "hello world"
23+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: String To Ascii
3+
description: Converts a string into ascii numbers
4+
author: Mcbencrafter
5+
tags: string,ascii,encoding,conversion
6+
---
7+
8+
```java
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public static List<Integer> stringToAscii(String text) {
13+
List<Integer> asciiCodes = new ArrayList<>();
14+
15+
for (char character : text.toCharArray()) {
16+
asciiCodes.add((int) character);
17+
}
18+
19+
return asciiCodes;
20+
}
21+
22+
// Usage:
23+
System.out.println(stringToAscii("hello world")); // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
24+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: String To Unicode
3+
description: Converts characters of a string into their unicode representation
4+
author: Mcbencrafter
5+
tags: string,unicode,encoding,conversion
6+
---
7+
8+
```java
9+
public static String stringToUnicode(String text) {
10+
StringBuilder unicodeText = new StringBuilder();
11+
12+
for (char character : text.toCharArray()) {
13+
unicodeText.append(String.format("\\u%04x", (int) character));
14+
}
15+
16+
return unicodeText.toString();
17+
}
18+
19+
// Usage:
20+
System.out.println(stringToUnicode("hello world")); // \u0068\u0065\u006C\u006C\u006F\u0020\u0077\u006F\u0072\u006C\u0064
21+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Unicode To String
3+
description: Converts a unicode String into its normal representation
4+
author: Mcbencrafter
5+
tags: string,unicode,encoding,decoding,conversion
6+
---
7+
8+
```java
9+
public static String unicodeToString(String unicode) {
10+
StringBuilder string = new StringBuilder();
11+
String[] hex = unicode.split("\\\\u");
12+
13+
for (int symbol = 1; symbol < hex.length; symbol++) {
14+
int data = Integer.parseInt(hex[symbol], 16);
15+
string.append((char) data);
16+
}
17+
18+
return string.toString();
19+
}
20+
21+
// Usage:
22+
System.out.println(unicodeToString("\\u0068\\u0065\\u006c\\u006c\\u006f\\u0020\\u0077\\u006f\\u0072\\u006c\\u0064")); // "hello world"
23+
```

0 commit comments

Comments
 (0)