EvilZone

Programming and Scripting => Beginner's Corner => : blackeagle February 21, 2015, 12:17:13 PM

: Java noob question
: blackeagle February 21, 2015, 12:17:13 PM
am working on making a binary converting program just to train myself .
well my question is if i can use a function inside another function in java in my case i made function called hexToBinary it converts from hex to binary and return a binary number as a String in a variable called result.
and i have a function called binaryToDecimal that takes a  binary and transform it to decimal
so far everything works but now i want to make a new function called hexToDecimal basicly going from hex to decimal could be going from hex to binary then from binary to decimal am i right till here ?
 
:
public static void hexToDecimal(String a){
  hexToBinary(a);
binaryToDecimal(result);

  }
the hexToBinary works fine but i cant get the variable result from it as a parameter in binaryToDecimal is there any way i could use it o this is impossible to do ?
: Re: Java noob question
: Deque February 21, 2015, 01:18:01 PM
Your other methods hexToBinary and binaryToDecimal must return their result so you can assign it to a variable for re-use. Your hexToDecimal would look like this:

:
public static String hexToDecimal(String hex){
String binary = hexToBinary(hex); //save result in binary
String decimal = binaryToDecimal(binary); //save result in decimal
return decimal; //return result
 }

Or more concise:

:
public static String hexToDecimal(String hex) {
         return binaryToDecimal(hexToBinary(hex));
 }
: Re: Java noob question
: blackeagle February 21, 2015, 04:28:06 PM
works great thank you @Deque this will makes it lots easier + lookks like the second method you gave me is better to use
: Re: Java noob question
: Matriplex February 21, 2015, 07:39:28 PM
You can also go the C++esque route and store the result in the variable you passed. However that's going heavily against Java conventions AFAIK.
: Re: Java noob question
: Deque February 21, 2015, 08:47:28 PM
You can also go the C++esque route and store the result in the variable you passed. However that's going heavily against Java conventions AFAIK.

Not directly. All they have to say about return statements is this: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-137265.html#333

Sometimes I do store it in a variable, if I think giving it a name makes the code better readable.
: Re: Java noob question
: blackeagle February 21, 2015, 11:01:04 PM
:
import java.util.Scanner;

public class conversion {

    public static boolean isBinary(String a) {
        for (int i = 0; i < a.length(); i++) {
            if ((a.charAt(i) != '0') && (a.charAt(i) != '1')) {
                return false;
            }
        }
        return true;
    }

    public static int binaryToDecimal(String a) {
        int decimal = 0;
        for (int i = a.length() - 1; i >= 0; i--) {
            if (a.charAt(i) == '1') {
                decimal += Math.pow(2, (a.length()) - i - 1);
            }
        }
        return decimal;
    }

    public static String binaryToHex(String a) {
        if (a.length() % 4 == 1) {
            a = "000" + a;
        }
        if (a.length() % 4 == 2) {
            a = "00" + a;
        }
        if (a.length() % 4 == 3) {
            a = "0" + a;
        }
        String result = "";
        String hex[] = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
        String binary[] = new String[]{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
        for (int i = 0; i < a.length(); i += 4) {
            for (int j = 0; j < binary.length; j++) {
                if (binary[j].equals(a.substring(i, i + 4))) {
                    result += hex[j];
                }
            }
        }
        return result;
    }

    public static String binaryToOctal(String a) {
        if (a.length() % 3 == 1) {
            a = "00" + a;
        }
        if (a.length() % 3 == 2) {
            a = "0" + a;
        }
        String result = "";
        String octal[] = new String[]{"0", "1", "2", "3", "4", "5", "6", "7"};
        String binary[] = new String[]{"000", "001", "010", "011", "100", "101", "110", "111"};
        for (int i = 0; i < a.length(); i += 3) {
            for (int j = 0; j < binary.length; j++) {
                if (binary[j].equals(a.substring(i, i + 3))) {
                    result += octal[j];
                }
            }
        }
        return result;
    }
   
    public static boolean isHex(String a){
  try {
    Long.parseLong(a, 16);
    return true;
  }
  catch (NumberFormatException ex) {
    // Error handling code...
    return false;
  }}
 
  public static String hexToBinary(String a){
  String result="";
  char hex[] = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F'};
  String binary[] = new String[]{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  for(int i=0;i<a.length();i++){
  for(int j=0;j<hex.length;j++){
  if(Character.toUpperCase(a.charAt(i))==(hex[j])){
  result+=binary[j];
  }}
  }return result;}
 
  public static int hexToDecimal(String a){
return binaryToDecimal(hexToBinary(a));
  }
 
  public static String hexToOctal(String a){
  return binaryToOctal(hexToBinary(a));
  }
 
  public static boolean isOctal(int a){
  if((a<0)||(a>7)){return false;}
  return true;
  }
 
  public static String octalToBinary(int a){
  int octal[] = new int[]{'0', '1', '2', '3', '4', '5', '6', '7'};
  String binary[] = new String[]{"000", "001", "010", "011", "100", "101", "110", "111"};
  String c=Integer.toString(a); String result="";
  for(int i=0;i<c.length();i++){
  for(int j=0;j<octal.length;j++){
  if(c.charAt(i)==octal[j]){
  result+=binary[j];
  }}}
  return result;
  }
 
  public static String octalToHex(int a){
  return binaryToHex(octalToBinary(a));
  }
 
  public static int octalToDecimal(int a){
  return binaryToDecimal(octalToBinary(a));
  }
 
  public static String decimalToBinary(double a){
  String result="";
  while(a>0){
  if(a%2==1){result=1+result;}
  else {result=0+result;}
  a=a/2;}
  return result;
  }
 
  public static String decimalToHex(double a){
  return binaryToHex(decimalToBinary(a));
  }
 
  public static String decimalToOctal(double a){
  return binaryToOctal(decimalToBinary(a));
  }

this is what i did so far pls @Deque when you have some free time take a look at it and let me know if i need to fix anything or add anything to make it better !!
: Re: Java noob question
: Deque February 22, 2015, 01:21:27 PM
Your parameter types are not coherent.

Why would you pass a String for binaryToDecimal, but int for octalToBinary?
With your present octalToBinary function you are not able to convert an octal 10 or anything above that. This is a flaw.

It is a similar problem with isOctal(int a). An integer is not octal, decimal or binary, only its representation is. You can represent every integer in the octal system, so testing isOctal with an int really does not make any sense.

:
public static String decimalToBinary(double a){
  String result="";
  while(a>0){
  if(a%2==1){result=1+result;}
  else {result=0+result;}
  a=a/2;}
  return result;
  }
 
  public static String decimalToHex(double a){
  return binaryToHex(decimalToBinary(a));
  }
 
  public static String decimalToOctal(double a){
  return binaryToOctal(decimalToBinary(a));
  }

Your conversion does not work with double.
Try to convert the decimal 0.1 to binary. You may use this to check how the result should look like: http://www.exploringbinary.com/binary-converter/

Don't allow to pass a double if your conversion only works with integers. If you need a double for any reason you can still cast the type within your method.
: Re: Java noob question
: blackeagle February 22, 2015, 08:06:37 PM
ok thank you very much i'll try and see what i can do about it when i have some free time