-2
import java.lang.Integer;
import java.util.Arrays;

public class Decimal {

    //initialize intance variables
    private int decimal;
    private String hex;

    public static void toHex(String s) {
        int decimal = Integer.parseInt(s); //converts the s string into an int for binary conversion. 
        String hex = null;
        int[] binNum = new int[16];
        int[] binNumNibble = new int[4]; //A nibble is four bits.
        int nibbleTot = 0;
        char hexDig = '\0';
        char[] cvtdHex = new char[4];
        StringBuffer result = new StringBuffer();

        for(int a = 32768; a == 1; a /= 2) { //32768 is the value of the largest bit.
            int b = 0;//Will top at 15 at the end of the for loop. 15 references the last  spot in the binNum array. 
            if(decimal > a) {
                decimal -= a;
                binNum[b++] = 1;//Arrays have a default value of zero to all elements.     This provides a parsed binary number.
            }
        }

        for(int a = 0; a == 15; a += 3) {
            //Copies pieces of the binary number to the binNumNibble array. .arraycopy is used in java 1.5 and lower.
            //Arrays.copyOfRange is used in java 1.5 and higher.
            System.arraycopy(binNum, a, binNumNibble, 0, 4);

            for(int b = 8; b == 1; a += 3) {
                int c = 0;
                nibbleTot += binNumNibble[c++];

                //Converts the single hex value into a hex digit.
                if(nibbleTot >= 1 && nibbleTot <= 9) {
                    hexDig += nibbleTot;
                } else if(nibbleTot == 10) {
                    hexDig = 'A';
                } else if(nibbleTot == 11) {
                    hexDig = 'B';
                } else if(nibbleTot == 12) {
                    hexDig = 'C';
                } else if(nibbleTot == 13) {
                    hexDig = 'D';
                } else if(nibbleTot == 14) {
                    hexDig = 'E';
                } else if(nibbleTot == 15) {
                    hexDig = 'F';
                }

                cvtdHex[c++] = hexDig;
            }
        }
        //return hex = new String(cvtdHex);
        hex = new String(cvtdHex);
        System.out.print("Hex: " + hex);
    }
}

I can't seem to figure out why variable hex is returned as a blank variable. I've been using System.out.print(); in each for loop and none of them are used, giving me the impression that the for loops are being skipped entirely, but I don't understand why and I'm on a time limit.

Any help is much appreciated, but please don't just paste code. I need to understand this for my computer science class!

Mike Koch
  • 1,540
  • 2
  • 17
  • 23
Karthag
  • 1
  • 1
  • 2

3 Answers3

4

yes, your for loops ARE being skipped, since the second part of the for statement is not the break condition but the condition that has to be fullfilled for the loop to run.

So it is NOT

for(a = 0; a == 15; a += 3)

but

for(a = 0; a <= 15; a += 3)

and so on...

Roxxorfreak
  • 380
  • 2
  • 10
  • Thank you! This solved my problem, and now I get to move on to another problem that has made itself evident. – Karthag Feb 02 '14 at 03:00
1

The for loops wont execute because of the double ==

  • 2
    That is a terrible answer. – OldProgrammer Feb 02 '14 at 01:52
  • 2
    Well, he did "technically" answer the OP's question, but some clarification may have been more helpful for the OP. – Mike Koch Feb 02 '14 at 01:53
  • 2
    @StephenCorcoran To make this answer clearer don't just point mistake. Also add its explanation and possible solution. Try describing how how condition in `for` loop works. What value does it need to have to continue and when it will stop. – Pshemo Feb 02 '14 at 02:00
1

How about

String.format("%h", 256)
steffen
  • 16,138
  • 4
  • 42
  • 81
  • Since this is for his computer class I guess the goal is the implementation of the algorithm and not the result :-) – Roxxorfreak Feb 02 '14 at 01:55