Java string and ASCII transcoding

Source: Internet
Author: User

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

1) Java method for turning strings into ASCII

 Public Staticstring Stringtoascii (String value) {StringBuffer SBU=NewStringBuffer (); Char[] chars =Value.tochararray ();  for(inti = 0; i < chars.length; i++) {          if(I! = chars.length-1) {sbu.append (int) Chars[i]. Append (","); }          Else{sbu.append (int) chars[i]); }      }      returnsbu.tostring (); }  

2) Java method for turning ASCII into a string

 Public Static string asciitostring (string value)  {      new  stringbuffer ();       = Value.split (",");        for (int i = 0; i < chars.length; i++) {          sbu.append ((char) Integer.parseint ( Chars[i]));      }       return sbu.tostring ();  }  

Main method Test:

 Public Static void Main (string[] args) {                      = "{name:1234,password:4444}";               = stringtoascii (str);              System.out.println (Asciiresult);               = asciitostring (asciiresult);              System.out.println (Stringresult);             }    

Output:

123,110,97,109,101,58,49,50,51,52,44,112,97,115,115,119,111,114,100,58,52,52,52,52,125{name:1234, password:4444}

Java string and ASCII transcoding

Related Article

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.