Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class FunctionsHEX {
- HashMap<Integer, Character> decToHex = new HashMap<>();
- public static String fromBinToHex (String binary){
- int number = FunctionsBIN.fromBinToDec(binary.toCharArray());
- String hexString = "";
- int leftover;
- ArrayList<Integer> leftOvers = new ArrayList<>();
- HashMap<Integer, Character> decToHexValue = new HashMap<>();
- decToHexValue.put(0, '0');
- decToHexValue.put(1, '1');
- decToHexValue.put(2, '2');
- decToHexValue.put(3, '3');
- decToHexValue.put(4, '4');
- decToHexValue.put(5, '5');
- decToHexValue.put(6, '6');
- decToHexValue.put(7, '7');
- decToHexValue.put(8, '8');
- decToHexValue.put(9, '9');
- decToHexValue.put(10, 'A');
- decToHexValue.put(11, 'B');
- decToHexValue.put(12, 'C');
- decToHexValue.put(13, 'D');
- decToHexValue.put(14, 'E');
- decToHexValue.put(15, 'F');
- do {
- leftover = number % 16;
- number /= 16;
- leftOvers.add(leftover);
- } while (number > 0);
- for (int i = leftOvers.size() - 1; i >= 0; i--) {
- int integer = leftOvers.get(i);
- for (Map.Entry<Integer, Character> map : decToHexValue.entrySet()) {
- if (integer == map.getKey()) {
- hexString += map.getValue();
- }
- }
- }
- return hexString;
- }
- public static char[] fromHexToBinary (String hex)
- {
- String binary = "";
- HashMap<Character, String> hexToBinValue = new HashMap<>();
- hexToBinValue.put('0', "0000");
- hexToBinValue.put('1', "0001");
- hexToBinValue.put('2', "0010");
- hexToBinValue.put('3', "0011");
- hexToBinValue.put('4', "0100");
- hexToBinValue.put('5', "0101");
- hexToBinValue.put('6', "0110");
- hexToBinValue.put('7', "0111");
- hexToBinValue.put('8', "1000");
- hexToBinValue.put('9', "1001");
- hexToBinValue.put('A', "1010");
- hexToBinValue.put('B', "1011");
- hexToBinValue.put('C', "1100");
- hexToBinValue.put('D', "1101");
- hexToBinValue.put('E', "1110");
- hexToBinValue.put('F', "1111");
- for (int i = 0; i < hex.length(); i++) {
- for (Map.Entry<Character, String> map : hexToBinValue.entrySet()) {
- if (map.getKey().equals(hex.charAt(i))) {
- binary += map.getValue();
- }
- }
- }
- ArrayList<Character> characterArrayList = new ArrayList<>();
- for (int i = 0; i < binary.length(); i++) {
- characterArrayList.add(binary.charAt(i));
- }
- if (characterArrayList.get(0) == '0') {
- characterArrayList.remove(0);
- }
- char[] charArray = new char[characterArrayList.size()];
- for (int i = 0; i < charArray.length; i++) {
- charArray[i] = characterArrayList.get(i);
- }
- return charArray;
- }
- public static int lettersInHexNumber (String hex,int i, int counter){
- if (i < hex.length()) {
- if (hex.charAt(i) >= 65 && hex.charAt(i) <= 70) {
- counter++;
- }
- i++;
- return lettersInHexNumber(hex, i, counter);
- }
- return counter;
- }
- public static String colorHexValueToDecimal (String hex){
- int red = 0;
- int green = 0;
- int blue = 0;
- for (int i = 0; i < 6; i += 2) {
- String str = hex.substring(i, i + 2);
- if (i < 2) {
- red = FunctionsBIN.fromBinToDec(fromHexToBinary(str));
- } else if (i >= 2 && i < 4) {
- green = FunctionsBIN.fromBinToDec(fromHexToBinary(str));
- } else {
- blue = FunctionsBIN.fromBinToDec(fromHexToBinary(str));
- }
- }
- return "rgb(" + red + ", " + green + ", " + blue + ')';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment