Conversion between day019 string and other types
Southerly wind 2021-06-04 10:35:12

String Conversion with different data types


String Conversion to basic data types

  • character string ---> Basic data type 、 Packaging

    • Integer The wrapper class public static int parseInt(String s): Can be made by “ Numbers ” The character string is converted to integer type .
    • Similarly , Use java.lang In bag Byte、Short、Long、Float、Double The corresponding class method can be called by “ Numbers ” character Composed string , Convert to the corresponding basic data type .
  • Basic data type 、 Packaging ---> character string

    • call String Class public String valueOf(int n) Can be int Type to string
    • Call the corresponding valueOf(byte b)、valueOf(long l)、valueOf(float f)、valueOf(double d)、valueOf(boolean b) It can be converted from the corresponding type of parameter to string .
 String str1="123";
int num=Integer.parseInt(str1);
String str2 = String.valueOf(num);

String And character array (char[]) Conversion between


  • String --> char[]: call String Of toCharArray() Method .
  • char[] ---> String: call String Constructor .
 public static void main(String[] args) {
String str1="abc123";
char[] charArray = str1.toCharArray();
for (char c : charArray) {
System.out.println(c);
}
char[] arr=new char[]{'h','e','l','l','o'};
String str2 = new String(arr);
System.out.println(str2);
}

Output results

a
b
c
1
2
3
hello

String And byte arrays (byte[]) Conversion between


  • Byte array (byte[])---> character string (String)

    • String(byte[]): Decode the specified... By using the platform's default character set byte Array , Make a new String.
    • String(byte[],int offset,int length): Use part of the specified byte array , That is, from the beginning of the array offset Began to take length To construct a string object .
  • character string (String)---> Byte array (byte[])

    • public byte[] getBytes(): Use the platform's default character set to set this String Encoded as byte Sequence , And store the results in a new byte Array .
    • public byte[] getBytes(String charsetName): Use the specified character set to String Code to byte Sequence , And store the results in a new byte Array .
public static void main(String[] args) throws UnsupportedEncodingException {
String str1="abc123 China ";
byte[] bytes = str1.getBytes();// Use the default character set , Encoding (UTF-8)
System.out.println(Arrays.toString(bytes));
byte[] gbks= str1.getBytes("gbk");// Use gbk The character set is encoded
System.out.println(Arrays.toString(gbks));
System.out.println("*****************************");
String str2 = new String(bytes);// Use the default character set , decode (UTF-8)
System.out.println(str2);
String str3 = new String(gbks);
System.out.println(str3);// Garbled , reason : The encoding set is not consistent with the decoding set
String str4 = new String(gbks, "gbk");
System.out.println(str4);// There is no garbled code , reason : The encoding set is consistent with the decoding set
}

Output results

[97, 98, 99, 49, 50, 51, -28, -72, -83, -27, -101, -67]
[97, 98, 99, 49, 50, 51, -42, -48, -71, -6]
*****************************
abc123 China
abc123�й�
abc123 China

  • code : character string ( Can understand )----> byte ( Binary data that you don't understand )
  • decode : byte ( Binary data that you don't understand )------> character string ( Can understand )
  • The statement : Encoding and decoding use different ways .

Silicon Valley

Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09