将Hex值从数组转换为ASCII字符以及如何消除空值?

时间:2014-01-10 12:06:42

标签: java arrays null

我正在尝试转换从我的方法中检索到的十六进制值,compareHexaRGB和ASCII字符,我想知道输出将产生什么。我不知道我做错了还是错过了代码。

用于将十六进制值转换为ASCII的extractMessage()方法的代码:

public class extractMessage
{
private static String[][] char1;
private static String[][] char2;
private static String[][] in;
private static String[][] combine;


public static void extractMessage(String[][] inn, String[][] comb)
{
    in = inn;
    combine = comb;
}

public static void printString2DArray(String[][] in) 
{
    for (int i = 0; i < in.length; i++) 
    {
        for(int j = 0; j < in[i].length; j++)
        {
            System.out.println(in[i][j] + " ");
        }
        System.out.println();
    }
}

public static void charExtract()
{
    compareHexaRGB hexRGB = new compareHexaRGB();

    char1 = hexRGB.getCheck_hex2();
    char2 = hexRGB.getCheck_hex4();

    combine = new String[char1.length][char1[0].length];

    for(int i = 0; i < char1.length; i++)
    {
        for(int j = 0; j < char1[i].length; j++)
        {
            //concatenate string
            combine[i][j] = char1[i][j] + char2[i][j];
        }
    }
    System.out.println("Char 1 + Char 2: ");
    printString2DArray(combine);
}

public static String convertHexToString()
{
    extractMessage em = new extractMessage();
    StringBuilder sb = new StringBuilder();
    StringBuilder temp = new StringBuilder();

    String out = em.charExtract(); //error stated incompatible types: void cannot be converted to String

    int decimal;

    for(int i = 0; i < out.length(); i += 2)
    {
        String output = out.substring(i, (i + 2));
        decimal = Integer.parseInt(output, 16);
        sb.append((char)decimal);

        temp.append(decimal);
    }
    System.out.println("Output: " + temp.toString());
    return sb.toString();
}
}

其次,我仍然无法从检索到的值中消除NULL值。有人说我需要添加一个位置来保存字符,在这种情况下是NULL值。我已经这样做了,但是当我尝试运行代码时,我们又回到这里,发生了nullpointerexception。我还是java新手,缺乏处理java数组和高级java的经验,但我很想学习。 Java是非常有趣的语言,我希望有一天我能掌握这门语言。

compareHexaRGB()的代码:

public class compareHexaRGB
{
private static int w;
private static int h;
private static BufferedImage img;
private static BufferedImage img2;
private static String[][] check_hex2;
private static String[][] check_hex4;
private static String[][] not_stega2;
private static String[][] not_stega4;

public static void compareHexaRGB(BufferedImage image, BufferedImage image2, int width, int height) throws IOException 
{
    w = width;
    h = height;
    img = image;
    img2 = image2;

}

public void check() throws IOException
    {
        getPixelRGB1 pixel = new getPixelRGB1();
        getPixelData1 newPD = new getPixelData1();

        int[] rgb;
        int count = 0;

        int[][] pixelData = new int[w * h][3];
        check_hex2 = new String[w][h];
        check_hex4 = new String[w][h];

        for(int i = 0; i < w; i++) 
        {
            for(int j = 0; j < h; j++)
            {
                rgb = newPD.getPixelData(img, i, j);

                for(int k = 0; k < rgb.length; k++)
                {
                    pixelData[count][k] = rgb[k];
                }

                    if(pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]))
                    {
                        System.out.println("\nPixel values at position 2 are the same." + "\n" + pixel.display_imgHex2()[i][j] + "  " + pixel.display_img2Hex2()[i][j]);
                        not_stega2[i][j] = pixel.display_img2Hex2()[i][j]; // i've done the same as check_hex2 and check_hex4 method but why the error still occur?
                    }
                    if(pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))
                    {
                        System.out.println("\nPixel values at position 4 are the same." + "\n" + pixel.display_imgHex4()[i][j] + "  " + pixel.display_img2Hex4()[i][j]);
                        not_stega4[i][j] = pixel.display_img2Hex4()[i][j];
                    }
                    if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]))
                    {
                        System.out.println("\nPixel values at position 2 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + "  " + pixel.display_img2Hex2()[i][j]);
                        check_hex2[i][j] = pixel.display_img2Hex2()[i][j];
                        System.out.println("\nOutput Hex 2: " + check_hex2[i][j]);
                    }
                    if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))
                    {
                        System.out.println("\nPixel values at position 4 are not the same." + "\n" + pixel.display_imgHex4()[i][j] + "  " + pixel.display_img2Hex4()[i][j]);
                        check_hex4[i][j] = pixel.display_img2Hex4()[i][j];
                        System.out.println("\nOutput Hex 4: " + check_hex4[i][j]);
                    }
                    if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])))
                    {
                        System.out.println("\nOne of the pixel values at position 2 and 4 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + "  " + pixel.display_img2Hex2()[i][j] + "\n" + pixel.display_imgHex4()[i][j] + "  " + pixel.display_img2Hex4()[i][j]);

                        if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j])))
                        {
                            check_hex2[i][j] = pixel.display_img2Hex2()[i][j];
                            System.out.println("\nOutput Hex 2: " + check_hex2[i][j]);
                        }

                        if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]) || (pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])))
                        {
                            check_hex4[i][j] = pixel.display_img2Hex4()[i][j];
                            System.out.println("\nOutput Hex 4: " + check_hex4[i][j]);
                        }

                    }
                count++;
                System.out.println("\nOutput Count: " + count);
            }

        }

    }

public String[][] getCheck_hex2()
{
    return check_hex2;
}

public String[][] getCheck_hex4()
{
    return check_hex4;
}

public String[][] getCheck_notStega2()
{
    return not_stega2;
}

public String[][] getCheck_notStega4()
{
    return not_stega4;
}
 }

希望快速消除这些问题。感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

public static void charExtract()
{ ...
}

String out = em.charExtract(); //error stated incompatible types
显然不匹配。此外,为什么要尝试在实例 charExtract()上调用静态方法em

你在那些String[][]数组中存储了什么?

char1 = hexRGB.getCheck_hex2();
char2 = hexRGB.getCheck_hex4();

我们无法帮助您将内容转换为任何内容。

目前我认为你真的不想处理“十六进制”数字;在int上执行计算要比在数字的任何字符串表示上更容易执行。

答案 1 :(得分:0)

正如我所看到的,您的代码中有两个缺陷::

1。如果charExtract()方法是静态的,那么您需要以静态方式访问它,如::

String out = extractMessage.charExtract(); 

2. 其次,当您将charExtract()中的值存储到String变量“out”时 所以你需要从charExtract()方法返回一个字符串,将其声明为

public static String charExtract()
{ ...
}

并从charExtract返回一些值,如:

return combine[o][1];