Java string __java with ASCII code

Source: Internet
Author: User
Tags stringbuffer

1. As a result of the project encountered in the server-side jar package program, send a message to the front desk, the previous paragraph received the message garbled situation, so take the message before sending the message string into an ASCII code before sending to the front desk, The foreground takes a solution that turns the ASCII code into a string before receiving the background message.

1 Java method for converting strings to ASCII

public static string Stringtoascii (string value) {StringBuffer SBU = new StringBuffer (); char[] chars = Value.tochararray (); for (int i = 0; i < chars.length i++) {if (I!= chars.length-1) {sbu.append ((int) chars[i]). Append (",");} else {SB U.append ((int) chars[i]); } return sbu.tostring (); }

2 Java method for converting ASCII to strings

public static string Asciitostring (string value) {StringBuffer SBU = new StringBuffer (); string[] chars = Value.split (","); for (int i = 0; i < chars.length i++) {sbu.append ((char) integer.parseint (chars[i)), return sbu.tostring (); }

Test methods and Results

public static void Main (string[] args) {
String str = "Peng Yujun";
String Asciiresult = stringtoascii (str);
System.out.println (Asciiresult);
String Stringresult = asciitostring (Asciiresult);
System.out.println (Stringresult);
}

Console output 24429,29577,20891 Peng Yujun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.