convert To String to Byte array vis its char array - Java java.lang

Java examples for java.lang:char Array

Description

convert To String to Byte array vis its char array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String source = "java2s.com";
        System.out.println(java.util.Arrays
                .toString(convertToBytes(source)));
    }// w w w.  j a  v  a 2  s.  c om

    public static byte[] convertToBytes(String source) {
        char[] chars = source.toCharArray();
        byte[] result = new byte[chars.length];
        for (int i = 0; i < chars.length; i++) {
            result[i] = (byte) chars[i];
        }
        return result;
    }
}

Related Tutorials