集册 Java实例教程 将字符串转换为二进制字节数组

将字符串转换为二进制字节数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

473
将字符串转换为二进制字节数组
/** 来 自 时代Java - N o w  J a v a . c o m**/


//package com.nowjava;


public class Main {

    public static void main(String[] argv) throws Exception {

        String src = "nowjava.com";

        System.out.println(java.util.Arrays.toString(asBin(src)));

    }


    private static byte[] asBin(String src) {

        if (src.length() < 1)

            return null;

        byte[] encrypted = new byte[src.length() / 2];

        for (int i = 0; i < src.length() / 2; i++) {

            int high = Integer

                    .parseInt(src.substring(i * 2, i * 2 + 1), 16);

            int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2)
展开阅读全文