English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java program converts string to byte array

C'est notre chaîne de caractères.

String str = "L'Asie est un continent !";

Maintenant, utilisons le tableau de bytes etgetBytes()Méthode pour réaliser notre objectif.

byte[] byteVal = str.getBytes();

Maintenant, si nous devons obtenir la longueur d'un tableau, il retournera cette longueur, comme montré dans l'exemple complet ci-dessous :

Exemple

public class Demo {
   public static void main(String args[]) {
      String str = "L'Asie est un continent !";
      System.out.println(str);
      //Convertir en tableau de bytes
      byte[] byteVal = str.getBytes();
      //Obtenir la longueur
      System.out.println(byteVal.length);
   }
}

Résultat de la sortie

L'Asie est un continent !
20