繁体   English   中英

将十六进制值从数组转换为ASCII字符,以及如何消除空值?

[英]Convert Hex value to ASCII character from array and how to eliminate null value?

我在中间,试图将我从我的方法中检索到的十六进制值(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;
}
 }

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

public static void charExtract()
{ ...
}

String out = em.charExtract(); //error stated incompatible types

显然不匹配。 此外,为什么要尝试在实例 em上调用静态方法charExtract()

您在这些String[][]数组中存储什么?

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

不知道它们应包含的内容,我们无法帮助您将其内容转换为任何内容。

目前,我假设您真的不想处理“十六进制”数字; 与对任何数字的String表示相比,在int上执行计算要容易得多。

如我所见,您的代码中存在2个缺陷:

1.如果charExtract()方法是静态的,则需要以静态方式访问它:

String out = extractMessage.charExtract(); 

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

public static String charExtract()
{ ...
}

并从charExtract返回一些值,例如:

return combine[o][1];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM