0

So I'm working on a method in Java that basically takes a random string of letters then goes through the method and changes the string into parity bits, which is basically converting each character to its numeric value in binary.

This is what I have:

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
    }
}

This is the code that converts the string into 4x4 matrices:

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

     }
 }

So basically if the input string is: WVOGJTXQHUHXICWYYMGHTRKQHQPWKYVGLPYSPWGOINTOFOPMO

The output should look like this:

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

I just need help combining the codes! I can get them working separately but I cant get the output I need. Please show how you would code this.

Andreas
  • 154,647
  • 11
  • 152
  • 247
L.redir
  • 65
  • 1
  • 2
  • 9

1 Answers1

1

Don't use StringBuffer. Use StringBuilder instead.

Your printing loop is writing one letter at a time, separated by 3 spaces (and newlines). Letters in hex consist of two hex digits, as you already show in the desired output, so that won't work.

Your code prints blank lines at the end, which you probably don't want.

Integer.toHexString() will return a single digits if value is 0-15.

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 ");
        }
    }
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I see what you mean. Thanks for the advice! really helpful – L.redir Sep 18 '15 at 22:14
  • How would I change it so it would only take the string all at once instead of asking for four separate strings? The output is correct. Just unsure how I would change main – L.redir Sep 19 '15 at 00:34