Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static byte[] ConvertStringToHexBytes(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[] { (byte) 0x00, (byte) 0x00 };
        for (int i = 0; i <= 1; i++) {

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

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

                }
            }
        }
        ConvertedString[0] = (byte) result;

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

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

                }
            }
        }
        ConvertedString[1] = (byte) result;

        return ConvertedString;
    }
}