簡體   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