MilaDimitrovaa

Hex - functions

Nov 10th, 2021
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. package com.company;
  2. import java.util.*;
  3.  
  4. public class FunctionsHEX {
  5.     HashMap<Integer, Character> decToHex = new HashMap<>();
  6.  
  7.  
  8.         public static String fromBinToHex (String binary){
  9.             int number = FunctionsBIN.fromBinToDec(binary.toCharArray());
  10.  
  11.             String hexString = "";
  12.             int leftover;
  13.             ArrayList<Integer> leftOvers = new ArrayList<>();
  14.  
  15.  
  16.             HashMap<Integer, Character> decToHexValue = new HashMap<>();
  17.             decToHexValue.put(0, '0');
  18.             decToHexValue.put(1, '1');
  19.             decToHexValue.put(2, '2');
  20.             decToHexValue.put(3, '3');
  21.             decToHexValue.put(4, '4');
  22.             decToHexValue.put(5, '5');
  23.             decToHexValue.put(6, '6');
  24.             decToHexValue.put(7, '7');
  25.             decToHexValue.put(8, '8');
  26.             decToHexValue.put(9, '9');
  27.             decToHexValue.put(10, 'A');
  28.             decToHexValue.put(11, 'B');
  29.             decToHexValue.put(12, 'C');
  30.             decToHexValue.put(13, 'D');
  31.             decToHexValue.put(14, 'E');
  32.             decToHexValue.put(15, 'F');
  33.  
  34.  
  35.             do {
  36.                 leftover = number % 16;
  37.                 number /= 16;
  38.  
  39.                 leftOvers.add(leftover);
  40.  
  41.             } while (number > 0);
  42.  
  43.  
  44.             for (int i = leftOvers.size() - 1; i >= 0; i--) {
  45.                 int integer = leftOvers.get(i);
  46.  
  47.                 for (Map.Entry<Integer, Character> map : decToHexValue.entrySet()) {
  48.                     if (integer == map.getKey()) {
  49.                         hexString += map.getValue();
  50.                     }
  51.                 }
  52.             }
  53.  
  54.  
  55.             return hexString;
  56.         }
  57.  
  58.  
  59.         public static char[] fromHexToBinary (String hex)
  60.         {
  61.             String binary = "";
  62.  
  63.             HashMap<Character, String> hexToBinValue = new HashMap<>();
  64.             hexToBinValue.put('0', "0000");
  65.             hexToBinValue.put('1', "0001");
  66.             hexToBinValue.put('2', "0010");
  67.             hexToBinValue.put('3', "0011");
  68.             hexToBinValue.put('4', "0100");
  69.             hexToBinValue.put('5', "0101");
  70.             hexToBinValue.put('6', "0110");
  71.             hexToBinValue.put('7', "0111");
  72.             hexToBinValue.put('8', "1000");
  73.             hexToBinValue.put('9', "1001");
  74.             hexToBinValue.put('A', "1010");
  75.             hexToBinValue.put('B', "1011");
  76.             hexToBinValue.put('C', "1100");
  77.             hexToBinValue.put('D', "1101");
  78.             hexToBinValue.put('E', "1110");
  79.             hexToBinValue.put('F', "1111");
  80.  
  81.             for (int i = 0; i < hex.length(); i++) {
  82.                 for (Map.Entry<Character, String> map : hexToBinValue.entrySet()) {
  83.                     if (map.getKey().equals(hex.charAt(i))) {
  84.                         binary += map.getValue();
  85.                     }
  86.                 }
  87.             }
  88.  
  89.             ArrayList<Character> characterArrayList = new ArrayList<>();
  90.  
  91.             for (int i = 0; i < binary.length(); i++) {
  92.                 characterArrayList.add(binary.charAt(i));
  93.             }
  94.  
  95.             if (characterArrayList.get(0) == '0') {
  96.                 characterArrayList.remove(0);
  97.             }
  98.  
  99.             char[] charArray = new char[characterArrayList.size()];
  100.  
  101.             for (int i = 0; i < charArray.length; i++) {
  102.                 charArray[i] = characterArrayList.get(i);
  103.             }
  104.  
  105.  
  106.             return charArray;
  107.         }
  108.  
  109.  
  110.         public static int lettersInHexNumber (String hex,int i, int counter){
  111.  
  112.             if (i < hex.length()) {
  113.                 if (hex.charAt(i) >= 65 && hex.charAt(i) <= 70) {
  114.                     counter++;
  115.                 }
  116.                 i++;
  117.  
  118.                 return lettersInHexNumber(hex, i, counter);
  119.             }
  120.  
  121.             return counter;
  122.         }
  123.  
  124.         public static String colorHexValueToDecimal (String hex){
  125.             int red = 0;
  126.             int green = 0;
  127.             int blue = 0;
  128.  
  129.  
  130.             for (int i = 0; i < 6; i += 2) {
  131.                 String str = hex.substring(i, i + 2);
  132.  
  133.                 if (i < 2) {
  134.                     red = FunctionsBIN.fromBinToDec(fromHexToBinary(str));
  135.                 } else if (i >= 2 && i < 4) {
  136.                     green = FunctionsBIN.fromBinToDec(fromHexToBinary(str));
  137.                 } else {
  138.                     blue = FunctionsBIN.fromBinToDec(fromHexToBinary(str));
  139.                 }
  140.             }
  141.  
  142.  
  143.             return "rgb(" + red + ", " + green + ", " + blue + ')';
  144.  
  145.         }
  146.     }
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment