将字符串值转换为ASCII

huangapple go评论93阅读模式
英文:

Convert a String Value to ASCII

问题

我想获取一个ASCII值(保存为字符串)并将其转换为字符以显示消息。我尝试了这个方法,但在声明int b时一直报告索引超出范围的错误。它还显示str和b没有值。

  1. String value = "104 101 108 108 111";
  2. char[] ch = new char[value.length()];
  3. for (int i = 0; i < value.length(); i++) {
  4. ch[i] = value.charAt(i);
  5. }
  6. System.out.println(ch.length);
  7. String ans = "";
  8. int i = 0;
  9. while (i+2 < ch.length) {
  10. int b = ch[i] + ch[i+1] + ch[i+2];
  11. String str = new Character((char) b).toString();
  12. System.out.println(str);
  13. System.out.println(b);
  14. ans = ans + str;
  15. i = i + 3;
  16. }
英文:

I want to take the value of a ASCII value(Saved as a string) and convert it to the character to reveal a message. I tried this and it keeps throwing an index out of bound at the declaration of the int b.It also shows that str and b do not have a value

String value = "104 101 108 108 111";

  1. char[] ch = new char[value.length()];
  2. for (int i = 0; i &lt; value.length(); i++) {
  3. ch[i] = value.charAt(i);
  4. }
  5. System.out.println(ch.length);
  6. String ans = &quot;&quot;;
  7. int i = 0;
  8. while (i+2 &lt; ch.length) {
  9. int b= ch[i]+ch[i++]+ch[i+2];
  10. String str = new Character((char) b).toString();
  11. System.out.println(str);
  12. System.out.println(b);
  13. ans = ans+str;
  14. i=i+3;
  15. }

答案1

得分: 2

使用字符串分割函数

  1. String value = "104 101 108 108 111";
  2. String[] arrOfStr = value.split(" ");
  3. String ans = "";
  4. for(String str : arrOfStr) {
  5. String str1 = Character.toString((char)Integer.parseInt(str));
  6. ans += str1;
  7. }
  8. System.out.println(ans); // 输出:hello
英文:

Using string split function

  1. String value = &quot;104 101 108 108 111&quot;;
  2. String[] arrOfStr = value.split(&quot; &quot;);
  3. String ans = &quot;&quot;;
  4. for(String str : arrOfStr) {
  5. String str1 = Character.toString((char)Integer.parseInt(str));
  6. ans += str1;
  7. }
  8. System.out.println(ans); // output: hello

答案2

得分: 0

我们可以使用Java 8的Streams将“Imperative”代码切换为“Declarative”代码。

需要注意的关键点:

  1. 声明式风格更易读且更易编写。
  2. 字符串连接器比简单的字符串拼接更快。
  3. 无需编写迭代器。
  1. import java.util.Arrays;
  2. public class Main {
  3. public static void main(String[] args) {
  4. String value = "104 101 108 108 111";
  5. Arrays.stream(value.split(" ")) // Starting a stream of String[]
  6. .mapToInt(Integer::parseInt) // mapping String to int
  7. .mapToObj(Character::toChars) // finding ASCII char from int
  8. .forEach(System.out::print); // printing each character
  9. }
  10. }

如果您希望将结果存储起来然后打印出来,可以按照以下方式进行操作。

  1. import java.util.Arrays;
  2. import java.util.stream.Collectors;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String value = "104 101 108 108 111";
  6. String result = Arrays.stream(value.split(" ")) // Starting a stream of String[]
  7. .mapToInt(Integer::parseInt) // mapping String to int
  8. .mapToObj(Character::toChars) // finding ASCII char from int
  9. .map(String::new) // convert char to String
  10. .collect(Collectors.joining()); // combining individual result using String Joiner
  11. System.out.println(result);
  12. }
  13. }
英文:

We can switch the Imperative code to Declarative code using Java 8 Streams.

Key points to observe:

  1. Declarative style is more readable and easy to write.
  2. String Joiner is faster than simple String Concatenation.
  3. No need to write an iterator.
  1. import java.util.Arrays;
  2. public class Main {
  3. public static void main(String[] args) {
  4. String value = &quot;104 101 108 108 111&quot;;
  5. Arrays.stream(value.split(&quot; &quot;)) // Starting a stream of String[]
  6. .mapToInt(Integer::parseInt) // mapping String to int
  7. .mapToObj(Character::toChars) // finding ASCII char from int
  8. .forEach(System.out::print); // printing each character
  9. }
  10. }

If you wish to store the result and then print it, this is how is done.

  1. import java.util.Arrays;
  2. import java.util.stream.Collectors;
  3. public class Main {
  4. public static void main(String[] args) {
  5. String value = &quot;104 101 108 108 111&quot;;
  6. String result = Arrays.stream(value.split(&quot; &quot;)) // Starting a stream of String[]
  7. .mapToInt(Integer::parseInt) // mapping String to int
  8. .mapToObj(Character::toChars) // finding ASCII char from int
  9. .map(String::new) // convert char to String
  10. .collect(Collectors.joining()); // combining individual result using String Joiner
  11. System.out.println(result);
  12. }
  13. }

答案3

得分: -1

  1. // 代码注释:
  2. // 有一个内置方法可以获得字符串的字符数组 `char[]`,因此下面这两个代码块是相同的:
  3. ```lang-java
  4. // 来自问题的代码
  5. char[] ch = new char[value.length()];
  6. for (int i = 0; i < value.length(); i++) {
  7. ch[i] = value.charAt(i);
  8. }
  9. // 使用内置方法
  10. char[] ch = value.toCharArray();

在循环时递增一个值时最好使用 for 循环。下面两种方式的循环行为相同,但 for 循环将循环逻辑放在一起:

  1. // 来自问题的代码
  2. int i = 0;
  3. while (i+2 < ch.length) {
  4. // 这里有一些代码
  5. i=i+3;
  6. }
  7. // 使用 for 循环
  8. for (int i = 0; i + 2 < ch.length; i=i+3) {
  9. // 这里有一些代码
  10. }

下面这行代码是完全错误的:

  1. int b= ch[i]+ch[i++]+ch[i+2];
  1. i++ 递增了 i 的值,但在表达式中使用的是递增前的值,这意味着如果在此行之前 i = 0,结果与以下代码相同:

    1. int b = ch[0] + ch[0] + ch[2];
    2. i = i + 1;

    您需要用 i + 1 替换 i++,并意识到它们是不同的

  2. 由于您不再在该语句中将 i 的值递增 1,因此循环必须从 i=i+3 更改为 i = i + 4,以正确跳过输入中的空格。

  3. ch[i] 的值是一个 char 值,通过使用 + 运算符将其扩展为 int 值。charint 值是 Unicode 代码点值,对于您的文本,它也是字符的 ASCII 码。

    这意味着如果 i = 0,在修复问题#1后,表达式将计算为:

    1. int b = ch[0] + ch[1] + ch[2];
    2. int b = `1` + `0` + `4`;
    3. int b = 49 + 48 + 52;
    4. int b = 149;

    这与在问题中运行代码的输出相匹配,其中第二个打印的数字是 149(在修复问题#1后)。

您真正想要的是获取子字符串 &quot;104&quot;,将其转换为数字,然后将该 ASCII 码值强制转换为 char,就像这样:

  1. String numberStr = value.substring(i, i + 3); // 例如:&quot;104&quot;
  2. int number = Integer.parseInt(numberStr); // 例如:104
  3. String str = String.valueOf((char) number); // 例如:&quot;h&quot;

有了这个,您就不再需要 char[],因此最终的代码将是:

  1. String value = &quot;104 101 108 108 111&quot;;
  2. String ans = &quot;&quot;;
  3. for (int i = 0; i + 2 < value.length(); i += 4) {
  4. String numberStr = value.substring(i, i + 3);
  5. int number = Integer.parseInt(numberStr);
  6. String str = String.valueOf((char) number);
  7. ans = ans + str;
  8. }
  9. System.out.println(ans);

输出

  1. hello
英文:

Comments to code:

There is a built-in method for for getting a char[] with the characters of a string, so the following two blocks of code are the same:

  1. // Code from question
  2. char[] ch = new char[value.length()];
  3. for (int i = 0; i &lt; value.length(); i++) {
  4. ch[i] = value.charAt(i);
  5. }
  6. // Using built-in method
  7. char[] ch = value.toCharArray();

It is better to use a for loop when increment a value while looping. The following two ways of writing the loop behave the same, but the for loop keeps the loop logic together:

  1. // Code from question
  2. int i = 0;
  3. while (i+2 &lt; ch.length) {
  4. // some code here
  5. i=i+3;
  6. }
  7. // Using for loop
  8. for (int i = 0; i + 2 &lt; ch.length; i=i+3) {
  9. // some code here
  10. }

The following line of code is entirely wrong:

  1. int b= ch[i]+ch[i++]+ch[i+2];
  1. i++ increments the value is i, but it is the value before the increment that is used in the expression, which means that if i = 0 before the line, the result is the same as this code:

    1. int b = ch[0] + ch[0] + ch[2];
    2. i = i + 1;

    You need to replace i++ with i + 1, and realize that those are not the same.

  2. Since you no longer increment the value of i by 1 in that statement, the loop much be changed from i=i+3 to i = i + 4, to correctly skip the spaces in the input.

  3. The value of ch[i] is a char value, which is widened to an int value by the use of the + operator. The int value of a char is the Unicode Code Point value, which for your text is also the same as the ASCII code for the character.

    This means that if i = 0, the expression would (after fixing issue #1) evaluate as:

    1. int b = ch[0] + ch[1] + ch[2];
    2. int b = `1` + `0` + `4`;
    3. int b = 49 + 48 + 52;
    4. int b = 149;

    That matches the output from running the code is in question, where the second printed number is 149 (after fixing issue #1).

What you really wanted was to get the substring &quot;104&quot; and convert that to a number, then cast that ASCII code value to a char, like this:

  1. String numberStr = value.substring(i, i + 3); // E.g. &quot;104&quot;
  2. int number = Integer.parseInt(numberStr); // E.g. 104
  3. String str = String.valueOf((char) number); // E.g. &quot;h&quot;

With that, you no longer need the char[], so the final code would be:

  1. String value = &quot;104 101 108 108 111&quot;;
  2. String ans = &quot;&quot;;
  3. for (int i = 0; i + 2 &lt; value.length(); i += 4) {
  4. String numberStr = value.substring(i, i + 3);
  5. int number = Integer.parseInt(numberStr);
  6. String str = String.valueOf((char) number);
  7. ans = ans + str;
  8. }
  9. System.out.println(ans);

Output

  1. hello

huangapple
  • 本文由 发表于 2020年7月26日 02:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63091745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定