Java - Looping and StringBuilders

109 views Asked by At

I Have run into a brickwall and I'm not sure how to continue.

Quick Rundown: My code is suppose to turn 4 Strings of Hexadecimals(ramError1-4) to Binary, manually. I Want to be able to loop 4 times and each time create a unique StringBuilder to continue to the next step.

I have tried over and over again the past two days to get it to work without success. Any advice would be appreciated. This is my code without my failed loops. Hex2 works as a test, but I want to be able to loop through HexCodes 1-4(ramError1-4)

So how do I loop through Hex1-4 while creating a StringBuilder?

Note: ramErrors1-4 do have strings in them I added earlier in the code by reading a file.

    String Hex1=ramError1;
    String Hex2=ramError2;
    String Hex3=ramError3;
    String Hex4=ramError4;

    StringBuilder hexstring1 = new StringBuilder();
    StringBuilder hexstring2 = new StringBuilder();
    StringBuilder hexstring3 = new StringBuilder();
    StringBuilder hexstring4 = new StringBuilder();


    for (int x = 0; x <= 8; x++)
    {

    if (Hex2.charAt(x) == 'A')
    {
        hexstring1.append("1010");

    }
    else if (Hex2.charAt(x) == 'B')
    {
        hexstring1.append("1011");

    }
    else if (Hex2.charAt(x) == 'C')
    {
        hexstring1.append("1100");
    }
    else if (Hex2.charAt(x) == 'D')
    {
        hexstring1.append("1101");
    }
    else if (Hex2.charAt(x) == 'E')
    {
        hexstring1.append("1110");
    }
    else if (Hex2.charAt(x) == 'F')
    {
        hexstring1.append("1111");
    } 
    else if (Hex2.charAt(x) == '0')
    {
        hexstring1.append("0000");
    }
    else if (Hex2.charAt(x) == '1')
    {
        hexstring1.append("0001");
    }
    else if (Hex2.charAt(x) == '2')
    {
        hexstring1.append("0010");
    }
    else if (Hex2.charAt(x) == '3')
    {
        hexstring1.append("0011");
    }
    else if (Hex2.charAt(x) == '4')
    {
        hexstring1.append("0100");
    }
    else if (Hex2.charAt(x) == '5')
    {
        hexstring1.append("0101");
    }
    else if (Hex2.charAt(x) == '6')
    {
        hexstring1.append("0110");
    }
    else if (Hex2.charAt(x) == '7')
    {
        hexstring1.append("0111");
    }
    else if (Hex2.charAt(x) == '8')
    {
        hexstring1.append("1000");
    }
    else if (Hex2.charAt(x) == '9')
    {
        hexstring1.append("1001");
    }
    else
    {
        System.out.println("error at char" + x );
    }
    }

    System.out.println("Hex To Binary is " + hexstring1.toString());
    System.out.println("Hex To Binary is " + hexstring2.toString());
    System.out.println("Hex To Binary is " + hexstring3.toString());
    System.out.println("Hex To Binary is " + hexstring4.toString());

vmina 3994433

2

There are 2 answers

0
Scary Wombat On

consider having a String array for the Hex

String hexes [] = new String []{ramError1, ramError2, ramError3, ramError4};

then move your for loop to a new method

StringBuilder doMyStuff (String hex) {

     StringBuilder bld = new StringBuilder ();

     for (int x = 0; x <= 8; x++)
     ....
}

This can then be called

  for (int loop = 0; loop < hexes.length; loop++) {

        StringBuilder result = doMyStuff (hexes[loop]);
0
Jack On

This is not the best way to convert an hexadecimal string to binary. Java already provides many utility functions:

long value = Long.parseInt(hexString, 16);
String result = Long.toBinaryString(value);

if (result.length() < 32)
{
  // pad with zeroes
}

Regarding your specific question you need loops and arrays to do what you need, something like

String[] hexStrings = new String[] { hexString1, hexString2, ... }
StringBuilder builders = new StringBuilder[4];

for (int i = 0; i < 4; ++i)
{
  builders[i] = new StringBuilder();
  String currentHext = hexStrings[i];

  for (int x = 0; x < 8; ++x)
  {
    ..