Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { public static String FormatValueByteWrite(String stringToFormat) { String stringFormated = stringToFormat; stringFormated = StringForceDigit(stringToFormat, 2); stringFormated = castHexKeyboard(stringFormated); return stringFormated.toUpperCase(); } public static String StringForceDigit(String sStringToFormat, int nbOfDigit) { String sStringFormated = sStringToFormat.replaceAll(" ", ""); if (sStringFormated.length() == 4) { return sStringFormated; } else if (sStringFormated.length() < nbOfDigit) { while (sStringFormated.length() != nbOfDigit) { sStringFormated = "0".concat(sStringFormated); } } return sStringFormated; } public static String castHexKeyboard(String sInput) { String sOutput = ""; sInput = sInput.toUpperCase(); char[] cInput = sInput.toCharArray(); for (int i = 0; i < sInput.length(); i++) { if (cInput[i] != '0' && cInput[i] != '1' && cInput[i] != '2' && cInput[i] != '3' && cInput[i] != '4' && cInput[i] != '5' && cInput[i] != '6' && cInput[i] != '7' && cInput[i] != '8' && cInput[i] != '9' && cInput[i] != 'A' && cInput[i] != 'B' && cInput[i] != 'C' && cInput[i] != 'D' && cInput[i] != 'E') { cInput[i] = 'F'; } sOutput += cInput[i]; } return sOutput; } }