【问题标题】:Changing output into 4x4 matrices将输出更改为 4x4 矩阵
【发布时间】:2015-12-16 03:18:15
【问题描述】:

所以我正在研究一种 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

我只需要帮助组合代码!我可以让它们单独工作,但我无法获得我需要的输出。请展示你将如何编码。

【问题讨论】:

  • 那么你会得到什么输出呢?
  • 输出:57564f474a5458514855485849435759594d474854524b51485150574b5956474c5059535057474f494e544f464f504d4f 作为一行,但我只需要将块输出放入 4x
  • 啊,您似乎也缺少实际的奇偶校验位。我认为你没有理解任务。

标签: java encryption methods parity


【解决方案1】:

不要使用StringBuffer。请改用StringBuilder

您的打印循环一次写入 一个 个字母,由 3 个空格(和换行符)分隔。十六进制中的字母由 两个 十六进制数字组成,正如您已经在所需的输出中显示的那样,因此这是行不通的。

您的代码在末尾打印空行,这可能是您不想要的。

如果值为 0-15,Integer.toHexString() 将返回单个数字。

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 ");
        }
    }
}

【讨论】:

  • 我明白你的意思。谢谢你的建议!真的很有帮助
  • 我将如何更改它以便它一次只获取所有字符串而不是要求四个单独的字符串?输出是正确的。只是不确定我将如何更改 main
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
  • 2014-09-05
  • 2010-11-12
  • 2012-05-24
  • 1970-01-01
  • 2017-08-21
相关资源
最近更新 更多