Advertisement
Visual-mov

ASCII to RGB Converter

Sep 20th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. public class ASCIItoRGB{
  5.  
  6.     public static void main(String args[]) {
  7.         String hexExtra = null;
  8.         // Input
  9.         String asciiValue = "This is a test";
  10.        
  11.         // ASCII to Hex
  12.         char[] c = asciiValue.toCharArray();
  13.         StringBuffer hexValue = new StringBuffer();
  14.         for(int i=0; i<c.length; i++) {
  15.             hexValue.append(Integer.toHexString((int)c[i]));
  16.         }
  17.         String hexString = hexValue.toString();
  18.        
  19.         // Hex to RGB
  20.         ArrayList<Color> rgbValues = new ArrayList<Color>();
  21.         while(hexString.length() >= 6) {
  22.             String tempval = "#" + hexString.substring(0,5);
  23.             rgbValues.add(Color.decode(tempval));
  24.             hexString = hexString.substring(tempval.length(), hexString.length());
  25.             if(hexString.length() < 6) {
  26.                 hexExtra = hexString;
  27.             }
  28.         }
  29.         // Output
  30.         System.out.println("ASCII VALUE: " + asciiValue);
  31.         System.out.println("CHAR ARRAY: " + Arrays.toString(c));
  32.         System.out.println("EXTRA DATA: " + hexExtra);
  33.         System.out.println("HEX VALUE: " + hexValue.toString());
  34.         System.out.println("RGB VALUES: " + rgbValues.toString());
  35.        
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement