package Main; import java.util.ArrayList; import java.io.*; public class Convert { /******************************************************************************** * This class is in control of all the converting and displaying the results. * ********************************************************************************/ public ArrayList FloatList; private String ArrayResultsString = ""; private String ReturnDebugArrayResultsString = ""; private String ProcessFileLoadedResultsString = ""; private String nl = "\r\n"; private String tab = "\t"; public Convert() { FloatList = new ArrayList(); // Preset Test Data. FloatList.add(Float.parseFloat("123.4578")); FloatList.add(Float.parseFloat("123.4578")); FloatList.add(Float.parseFloat("123.4578")); } // Run Array conversion. public void RunArrayTest(int Format) { ArrayResultsString = ""; int i = 0; int x = 0; int s = 0; while (i1 && CheckValid(FloatList.get(i))) { if (Format==1) { // Print Binary ArrayResultsString += result + nl; s++; } else { // Print Integers ArrayResultsString += SingleFloatToNumber(FloatList.get(i)) + nl; s++; } } else { x++; } } i++; } if (s>0) { ArrayResultsString += nl + "Successful Conversions: " + s; } if (x>0) { ArrayResultsString += nl + "Errors: " + x + ", view debug."; } } // Provide a debug of each conversion from float. public void RunDebugArrayTest(int Format) { ReturnDebugArrayResultsString = ""; int i = 0; int e = 0; while (i1) { try { // Try to Parse the Float, if successful then the input was a valid Float. Float.parseFloat(String.valueOf(SingleFloat)); // Add Float to List FloatList.add(SingleFloat); } catch (Exception e) { // Couldnt be added to list, most likely invalid float format. ProcessFileLoadedResultsString += "* The next float was invalid."; } } // Print Results Results++; ProcessFileLoadedResultsString += Results + ". \t " + SingleFloat + " \t\t " + SingleNumber + "\n"; } } else { // Read entire File its all been read and convert a float as one is found. // This generates more accurate results because it will parse 'Read Float' once per exact float found. while (ReadFileData.available() != 0 && Results1) { try { // Try to Parse the Float, if successful then the input was a valid Float. Float.parseFloat(String.valueOf(SingleFloat)); // Add Float to List FloatList.add(SingleFloat); } catch (Exception e) { // Couldnt be added to list, most likely invalid float format. ProcessFileLoadedResultsString += "* The next float was invalid."; } } // Print Results Results++; ProcessFileLoadedResultsString += Results + ". \t " + SingleFloat + " \t\t " + SingleNumber + " \n"; } } // Close Data Input Stream and File Input Stream. ReadFileData.close(); // Print Result Count Confirmation if (Results>0) { ProcessFileLoadedResultsString += "\n" + Results + " Results found."; } else { ProcessFileLoadedResultsString += "\nNo Results."; } } catch (Exception e) { // If there was an error trying to Stream at any given time, display error. ProcessFileLoadedResultsString += "\n\n An error occurred reading from this point on. \n Ensure the file read is genuinely a 32-bit Float File."; } } // Convert a Float to a Binary. public String SingleFloatToBinary(float input) { if (CheckValid(input)==true) { return Integer.toBinaryString(Float.floatToRawIntBits(input)); } else { return "Input invalid."; } } // Convert Float to Hex public String SingleFloatToHex(float input) { if (CheckValid(input)==true) { return Integer.toHexString(Float.floatToIntBits(input)); } else { return null; } } // Convert Float to Decimal public String SingleFloatToDec(float input) { if (CheckValid(input)==true) { return String.valueOf(Float.valueOf(input)); } else { return null; } } // Convert a Float to a Number. public String SingleFloatToNumber(float input) { if (CheckValid(input)==true) { return String.valueOf(Float.floatToRawIntBits(input)); } else { return null; } } // Convert a Number to Float public String SingleNumberToFloat(int input) { if (CheckValid(input)==true) { return String.valueOf(Float.intBitsToFloat(input)); } else { return null; } } // Check weather the string input is a float format. public boolean CheckValid(float float1) { try { Float.parseFloat(String.valueOf(float1)); return true; } catch (Exception e) { return false; } } // List Controls public void DeleteFromFloatList(int i) { FloatList.remove(i); } public void AddToFloatList(Float i) { FloatList.add(i); } // String Manipulation public String ExponentMantissa(String input) { String Start = input.substring(0, 8); String Finish = input.substring(8, input.length()); return Start + " - " + Finish; } }