简体   繁体   English

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

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

I'm in the middle trying to convert the hex value that I retrieved from my method, compareHexaRGB to ASCII character which I want to know what output is going to produce. 我在中间,试图将我从我的方法中检索到的十六进制值(compareHexaRGB)转换为ASCII字符,我想知道将产生什么输出。 I don't know if I'm doing it wrong or I missed to code somewhere. 我不知道我做错了还是错过了某个地方的代码。

Code for extractMessage() method to convert hex value to 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();
}
}

Second, I still cannot eliminate the NULL value from the retrieved value. 其次,我仍然无法从检索到的值中消除NULL值。 Someone said I need to add a place to hold the characters which in this case the NULL value. 有人说我需要添加一个位置来容纳字符,在这种情况下为NULL值。 I've done that but when I try to run the code then, here we go again, the nullpointerexception occur. 我已经做到了,但是当我尝试运行代码时,我们再次尝试执行nullpointerexception。 I'm still new to java and lack of experience handling with java arrays and advanced java but I'm eager to learn. 我还是Java的新手,也缺乏处理Java数组和高级Java的经验,但是我很想学习。 Java is very interesting language and I hope one day I could master on this language. Java是一种非常有趣的语言,我希望有一天我能精通这种语言。

Code for compareHexaRGB() : 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;
}
 }

Hoping to eliminate these problems fast. 希望能尽快消除这些问题。 Appreciate any help! 感谢任何帮助!

public static void charExtract()
{ ...
}

and

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

obviously don't match. 显然不匹配。 Besides, why are you trying to call the static method charExtract() upon the instance em ? 此外,为什么要尝试在实例 em上调用静态方法charExtract()

What do you store in those String[][] arrays? 您在这些String[][]数组中存储什么?

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

Without knowing what they are supposed to contain, we cannot help you convert their content to anything. 不知道它们应包含的内容,我们无法帮助您将其内容转换为任何内容。

At the moment I assume that you don't really want to deal with "hex" numbers; 目前,我假设您真的不想处理“十六进制”数字; calculations are much easier performed on int s than on any String representation of numbers. 与对任何数字的String表示相比,在int上执行计算要容易得多。

As i see there are 2 flaws in your code:: 如我所见,您的代码中存在2个缺陷:

1. If charExtract() method is static then you need to access it in a static way as:: 1.如果charExtract()方法是静态的,则需要以静态方式访问它:

String out = extractMessage.charExtract(); 

2. Secondly as you are storing the value from charExtract() to a String variable "out" so you need to return a string from the charExtract() method ,Declare it as 2.其次,当您将值从charExtract()存储到String变量“ out”时,您需要从charExtract()方法返回一个字符串,将其声明为

public static String charExtract()
{ ...
}

and return some value from charExtract like: 并从charExtract返回一些值,例如:

return combine[o][1];

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

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