0

だから私は基本的に文字のランダムな文字列を取り、メソッドを通過して文字列をパリティビットに変更するJavaのメソッドに取り組んでいます。これは基本的に各文字をバイナリの数値に変換します。

これは私が持っているものです:

public class ShiftData {
    //this is the method that where Logic is implemented..
    static String shiftRows(String text) {
        //character array to store given String elements into an array..
        char[] chs = text.toCharArray();
        StringBuffer samBuffer = new StringBuffer();
        //loop through the length of the character array..
        for (int i = 0; i < chs.length; i++) {
            //adding characters to the StringBuffer...
            samBuffer.append(Integer.toHexString((int) chs[i]));
//            here in the above statement toHexString method pads parity bit and converts to hexadecimal value..
        }
        return samBuffer.toString();//returning the value
    }
}

これは、文字列を 4x4 行列に変換するコードです。

if(text !=null && !text.isEmpty()) {
    int len = text.length();
    int rem = len %16;
    int padChars = 16-rem;


    for(int i =0; i < (len+padChars); i++) {
        if(i < len) {
            System.out.print(text.charAt(i)+ "   ");
        } else {
            System.out.print( "A   ");
        }

        if((i+1) % 4 == 0)
            System.out.println();
        if((i+1) % 16 == 0)
            System.out.println("\n");

     }
 }

したがって、基本的に入力文字列が次の場合:WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO

出力は次のようになります。

d7 56 cf 47

d4 d8 d1 ca

48 d8 48 55

59 c9 c3 d7


59 4d 47 48

d2 4b d1 d4

50 d7 48 d1

47 4b 59 56

cc 50 59 53

d7 47 cf 50

d4 cf c9 4e

4d c6 cf 50


cf 41 41 41

41 41 41 41

41 41 41 41

41 41 41 41

コードを組み合わせるのに助けが必要です!それらを別々に動作させることはできますが、必要な出力を得ることができません。これをどのようにコーディングするかを示してください。

4

1 に答える 1

1

使用しないでくださいStringBufferStringBuilder代わりに使用してください。

印刷ループは、3 つのスペース (および改行) で区切られて、一度に1文字ずつ書き込みます。16 進数の文字は、目的の出力に既に示されているように、 2 つの 16 進数で構成されているため、機能しません。

コードは最後に空白行を出力しますが、これはおそらく望ましくありません。

Integer.toHexString()値が 0 ~ 15 の場合、1 桁を返します。

static String shiftRows(String text) {
    char[] chs = text.toCharArray();
    StringBuilder samBuffer = new StringBuilder();
    for (int i = 0; i < chs.length; i++)
        samBuffer.append(String.format("%02x", (int)chs[i])); // always 2 hex digits, even for 0-15
    return samBuffer.toString();
}

public static void main(String[] args) {
    String text = shiftRows("WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO");
    if (text != null && ! text.isEmpty()) {
        int len = (text.length() + 31) / 32 * 32; // round up to next increment of 32 (16 hex pairs)
        for (int i = 0; i < len; i += 2) {
            if (i != 0 && i % 8 == 0) { // every 4 hex pairs, but not first time
                System.out.println();
                if (i % 32 == 0) // every 16 hex pairs
                    System.out.println();
            }
            if (i < text.length())
                System.out.print(text.substring(i, i + 2) + " ");
            else
                System.out.print("41 ");
        }
    }
}
于 2015-09-18T21:42:12.670 に答える