Java - 循环和StringBuilders

时间:2014-11-11 04:40:20

标签: java loops append stringbuilder charat

我遇到了砖墙,我不确定如何继续。

Quick Rundown:我的代码假设将4个十六进制字符串(ramError1-4)手动转换为Binary。 我想能够循环4次,每次创建一个独特的StringBuilder继续下一步。

过去两天我一遍又一遍地尝试让它无法成功。任何意见,将不胜感激。这是我没有失败循环的代码。 Hex2用作测试,但我希望能够遍历HexCodes 1-4(ramError1-4)

那么在创建StringBuilder时如何循环使用Hex1-4?

注意:ramErrors1-4确实有字符串,我之前在代码中通过读取文件添加了字符串。

    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 个答案:

答案 0 :(得分:0)

考虑为Hex

设置一个String数组
String hexes [] = new String []{ramError1, ramError2, ramError3, ramError4};

然后将您的for loop移至新方法

StringBuilder doMyStuff (String hex) {

     StringBuilder bld = new StringBuilder ();

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

这可以称为

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

        StringBuilder result = doMyStuff (hexes[loop]);

答案 1 :(得分:0)

这不是将十六进制字符串转换为二进制的最佳方法。 Java已经提供了许多实用程序功能:

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

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

关于您的具体问题,您需要循环和数组来执行您需要的操作,例如

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)
  {
    ..