0

I have a simple question. How to get one element from this 2d array:

String s2box[][]= {{"f","3","0","d"},{"1","d","e","8"},{"8","4","7","a"}, {"e","7","b","1"},{"6","f","a","3"},{"b","2","4","f"},{"3","8","d","4"},{"4","e","1","2"},{"9","c","5","b"},{"7","0","8","6"},{"2","1","c","7"},{"d","a","6","c"},{"c","6","9","0"},{"0","9","3","5"},{"5","b","2","e"},{"a","5","f","9"}};

I was trying like this, String sboxl = s2box[2,3];

I want to take this element for example and than convert it into binary number.

Why is this not working?

4
  • What you have is not a 2d array. its a jagged array (array of arrays). Commented May 12, 2011 at 22:38
  • 1
    String s2box[,] = {{...}}. The difference is in a jagged array, the rows can be of different lengths. in a 2d array, they cant. Commented May 12, 2011 at 22:42
  • How can I convert this one into 2d array? Commented May 12, 2011 at 22:50
  • String s2box[,]= {{"f","3","0","d"},{"1","d","e","8"},{"8","4","7","a"}, {"e","7","b","1"},{"6","f","a","3"},{"b","2","4","f"},{"3","8","d","4"},{"4","e","1","2"},{"9","c","5","b"},{"7","0","8","6"},{"2","1","c","7"},{"d","a","6","c"},{"c","6","9","0"},{"0","9","3","5"},{"5","b","2","e"},{"a","5","f","9"}}; I dont know enough java to tell you how to do it in general other than with for loops. as an aside, you might want to use a char[,] here, as it seems you dont really need more than one charachter. Commented May 12, 2011 at 22:51

2 Answers 2

2

You should try this:

String sbox1 = s2box[2][3];

The syntax you are using is incorrect for Java. Another way to look at it is like this:

String[] sboxTemp = s2box[2];
String sbox1 = sboxTemp[3];
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my God! I'm so sorry. I was tired and made mistake. Thank you.
2

To get the element from the array and convert it to the respective binary mode you need to do this:

String s2box[][]= {{"f","3","0","d"},{"1","d","e","8"},{"8","4","7","a"}, {"e","7","b","1"},{"6","f","a","3"},{"b","2","4","f"},{"3","8","d","4"},{"4","e","1","2"},{"9","c","5","b"},{"7","0","8","6"},{"2","1","c","7"},{"d","a","6","c"},{"c","6","9","0"},{"0","9","3","5"},{"5","b","2","e"},{"a","5","f","9"}};
String sbox1 = s2box[2][3];
String sbox1Binary = Integer.toBinaryString(Integer.valueOf(sbox1, 16).intValue());
System.out.println(sbox1Binary);

Output: 1010

3 Comments

I have problem when I want to get some number which is different than letter, for example 0. I don't get 0000. I just get 0.
Try System.out.printf("%04d\n", Integer.parseInt(sbox1Binary));.
It's not about printing. It's not problem. But I want to save it in real binary format which is 4 bits when converting from hex, because I want to use it later to add binary digits using xor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.