Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Utilities2 {
- public static String binToHex(String bin) {
- Hashtable<String, Character> htable = new Hashtable<>();
- htable.put("0000",'0');
- htable.put("0001",'1');
- htable.put("0010",'2');
- htable.put("0011",'3');
- htable.put("0100",'4');
- htable.put("0101",'5');
- htable.put("0110",'6');
- htable.put("0111",'7');
- htable.put("1000",'8');
- htable.put("1001",'9');
- htable.put("1010",'A');
- htable.put("1011",'B');
- htable.put("1100",'C');
- htable.put("1101",'D');
- htable.put("1110",'E');
- htable.put("1111",'F');
- String zeros = "";
- if(bin.length()%4 != 0) {
- for (int i = 0; i < 4-(bin.length()%4); i++) {
- zeros += "0";
- }
- }
- String[] arr = (zeros+bin).split("");
- String hexNumber = "";
- for (int i = 0; i < arr.length; i+=4) {
- String fourstr = arr[i] + arr[i+1] + arr[i+2] + arr[i+3];
- hexNumber += htable.get(fourstr);
- }
- return hexNumber;
- }
- public static String hexToBin(String hex) {
- char[] arr = hex.toCharArray();
- String[] strArr = {
- "0000", "0001", "0010", "0011",
- "0100", "0101", "0110", "0111",
- "1000", "1001", "1010", "1011",
- "1100", "1101", "1110", "1111"
- };
- String number = "";
- String n;
- for (int i = 0; i < arr.length; i++) {
- int num = arr[i] - '0';
- if(num >= 0 && num <= 10){
- n = strArr[num];
- }else{
- num = arr[i] - 'A' + 10;
- if(num >= 10 && num <= 15){
- n = strArr[num];
- }else{
- num = arr[i] - 'a' + 10;
- n = strArr[num];
- }
- }
- number += n;
- }
- boolean bool = false;
- int k = 0;
- String str = "";
- String[] arraySum = number.split("");
- //Махаме отпред нулите
- while(bool != true) {
- if (arraySum[k].equals("1")) {
- for (int j = k; j <= arraySum.length - 1; j++) {
- str += arraySum[j];
- }
- bool = true;
- }
- k++;
- }
- return str;
- }
- public static int hexToDecFix(String hex) {
- char[] arr = hex.toCharArray();
- int power = 1;
- int sum = 0;
- int n;
- for (int i = arr.length - 1; i >= 0; i--) {
- n = arr[i] - '0';
- if(!(n >= 0 && n < 10)){
- n = ((arr[i] - 'A') + 10);
- if(!(n >= 10 && n <= 15)){
- n = arr[i] - 'a' + 10;
- }
- }
- n *= power;
- sum += n;
- power *= 16;
- }
- return sum;
- }
- public static String hexToDecRGB(String hex) {
- String[] mainArr = hex.split("");
- String str = "rgb(";
- for (int i = 1; i < mainArr.length; i+=2) {
- str += hexToDecFix(mainArr[i] + mainArr[i+1]);
- if(i != 5){
- str += ", ";
- }
- }
- str += ")";
- return str;
- }
- public static String decToHex(int dec) {
- Stack<Integer> st = new Stack<>();
- while(dec != 0){
- int diff = dec%16;
- st.push(diff);
- dec = dec/16;
- }
- String str = "";
- while(st.size() != 0){
- int digit = st.pop();
- if(digit < 10){
- str += (digit + "");
- }else{
- str += (char)(digit - 10 + 'A');
- }
- }
- return str;
- }
- public static int letterCounter(char[] arr,int count,int sum) {
- if(count == -1){
- return sum;
- }
- int digit = arr[count] - '0';
- if(digit >= 0 && digit < 10){
- return letterCounter(arr,count-1,sum);
- }
- return letterCounter(arr,count-1,sum+=1);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////
- public class BinToHex {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String bin = scanner.nextLine();
- System.out.println(Utilities2.binToHex(bin));
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- public class HexLettersCounter {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int dec = scanner.nextInt();
- String str = Utilities2.decToHex(dec);
- char[] arr = str.toCharArray();
- System.out.println(Utilities2.letterCounter(arr,arr.length-1,0));
- }
- }
- //////////////////////////////////////////////////////////////////////////
- public class HexToDec {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String hex = scanner.nextLine();
- System.out.println(Utilities2.hexToDecFix(hex));
- }
- }
- //////////////////////////////////////////////////////////////////////////
- public class HexToBin {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String hex = scanner.nextLine();
- System.out.println(Utilities2.hexToBin(hex));
- }
- }
- //////////////////////////////////////////////////////////////////////////
- public class romanToDecimal {
- public static int romanToInteger(String roman) {
- Map<Character,Integer> numbersMap = new HashMap<>();
- numbersMap.put('I',1);
- numbersMap.put('V',5);
- numbersMap.put('X',10);
- numbersMap.put('L',50);
- numbersMap.put('C',100);
- numbersMap.put('D',500);
- numbersMap.put('M',1000);
- int result=0;
- for(int i=0;i<roman.length();i++)
- {
- char ch = roman.charAt(i); // Current Roman Character
- //Case 1
- if(i>0 && numbersMap.get(ch) > numbersMap.get(roman.charAt(i-1)))
- {
- result += numbersMap.get(ch) - 2*numbersMap.get(roman.charAt(i-1));
- }
- // Case 2: just add the corresponding number to result.
- else
- result += numbersMap.get(ch);
- }
- return result;
- }
- public static void main(String[] args) {
- // we take input as a String
- String romanNumber="MCMXCIV";
- int result = romanToInteger(romanNumber);
- System.out.println(romanNumber);
- System.out.println(result);
- }
- }
- //////////////////////////////////////////////////////////////////
- public class decimalToRoman {
- public static void main(String[] args) {
- // initialize reader for reading decimal number
- Scanner reader = new Scanner(System.in);
- System.out.println("Enter a number");
- // read number
- int number = reader.nextInt();
- methodOne(number);
- // close reader
- reader.close();
- }
- static void methodOne(int number) {
- // declare arrays for Roman numbers
- String[] thousands = { "", "M", "MM", "MMM" };
- String[] hundreds = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
- String[] tens = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
- String[] units = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" };
- int numberOfThousands = number / 1000;
- int numberOfHundreds = (number / 100) % 10;
- int numberOfTens = (number / 10) % 10;
- int numberOfUnits = number % 10;
- String romanNumber = thousands[numberOfThousands] + hundreds[numberOfHundreds]
- + tens[numberOfTens] + units[numberOfUnits];
- System.out.println(romanNumber);
- }
- }
- ///////////////////////////////////////////////////////////////////////////
- public class eightToSixteen {
- public static void main(String[] input)
- {
- String octnum, hexnum;
- int decnum;
- Scanner scan = new Scanner(System.in);
- octnum = scan.nextLine();
- decnum = Integer.parseInt(octnum, 8);
- hexnum = Integer.toHexString(decnum).toUpperCase();
- System.out.println(hexnum);
- }
- }
- /////////////////////////////////////////////////////////
- public class sixteenToEight {
- public static void main(String[] args) {
- int decimalNumber, a = 1, b;
- int[] octalNumber = new int[100];
- Scanner sc = new Scanner(System.in);
- String strHexNumber = sc.nextLine();
- decimalNumber = hexToDecimal(strHexNumber);
- while (decimalNumber != 0) {
- octalNumber[a++] = decimalNumber % 8;
- decimalNumber = decimalNumber / 8;
- }
- for (b = a - 1; b > 0; b--) {
- System.out.print(octalNumber[b]);
- }
- System.out.print("\n");
- sc.close();
- }
- public static int hexToDecimal(String str) {
- String strDigits = "0123456789ABCDEF";
- str = str.toUpperCase();
- int value = 0;
- for (int a = 0; a < str.length(); a++) {
- char ch = str.charAt(a);
- int deci = strDigits.indexOf(ch);
- value = 16 * value + deci;
- }
- return value;
- }
- }
- ////////////////////////////////////////////////////////////////////
- public class tenToThree {
- public static void convertToTernary(int N) {
- if (N == 0)
- return;
- int x = N % 3;
- N /= 3;
- if (x < 0)
- N += 1;
- convertToTernary(N);
- if (x < 0)
- System.out.print( x + (3 * -1));
- else
- System.out.print( x);
- }
- static void convert(int Decimal) {
- System.out.print("Ternary number of " +Decimal +" is: ");
- if (Decimal != 0) {
- convertToTernary(Decimal);
- }
- else
- System.out.println("0" );
- }
- public static void main (String[] args) {
- int Decimal = 2747;
- convert(Decimal);
- }
- }
- //////////////////////////////////////////////////////
- public class threeToTen {
- public static void convertToDecimal(int N) {
- System.out.print("Decimal number of " +
- N + " is: ");
- if (N != 0) {
- int decimalNumber = 0,
- i = 0, remainder;
- while (N != 0) {
- remainder = N % 10;
- N /= 10;
- decimalNumber += remainder *
- Math.pow(3, i);
- ++i;
- }
- System.out.print(decimalNumber + "\n");
- }
- else
- System.out.print("0" + "\n");
- }
- // Driver code
- public static void main(String[] args) {
- int Ternary = 10202202;
- convertToDecimal(Ternary);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment