Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static byte[] ConvertStringToHexBytesArray(String StringToConvert) {
        StringToConvert = StringToConvert.toUpperCase();
        StringToConvert = StringToConvert.replaceAll(" ", "");
        char[] CharArray = StringToConvert.toCharArray();
        char[] Char = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int result = 0;
        byte[] ConvertedString = new byte[StringToConvert.length() / 2];
        int iStringLen = (StringToConvert.length());

        for (int i = 0; i < iStringLen; i++) {
            for (int j = 0; j <= 15; j++) {
                if (CharArray[i] == Char[j]) {
                    if (i % 2 == 1) {
                        result = result + j;
                        j = 15;
                    }

                    else if (i % 2 == 0) {
                        result = result + j * 16;
                        j = 15;
                    }

                }
            }
            if (i % 2 == 1) {
                ConvertedString[i / 2] = (byte) result;
                result = 0;
            }
        }

        return ConvertedString;
    }
}