Read a number of 128 bits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • candexis
    New Member
    • Aug 2006
    • 51

    Read a number of 128 bits

    Please, I will like to Know how to read and store a variable of 128 bits and the split it in 16 variables of 8 bits each, and then create a matrix of 4x4 using each block of 8 bits, for instance:
    input = 32 43 F6 A8 88 5A 30 8D 31 31 98 A2 E0 37 07 34;

    the I need to create a matrix like this:


    input_M = { {32 88 31 E0}, {43 5A 31 37}, {F6 30 98 07}, {A8 8D A2 34}} ;

    Thank you very much,
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    just use a 2 dimensional byte array (with a dimension size of 4 in both directions) and read the data directly into it. There are no 128 bit built in data types on most PC based systems.

    char matrix[4][4];

    Comment

    • candexis
      New Member
      • Aug 2006
      • 51

      #3
      Well, the thing is that the user must enter this variable (128 bits), so I must ask for this variable, I then I need to store it somehow, and create the matix, so it would be somthing like this:

      cout << "Please enter the Key Number:"; //this is 128 bits
      cin >> ????????????? // So how can I get this

      and then I must create the matrix as I have described previous post. Thanks

      Comment

      • D_C
        Contributor
        • Jun 2006
        • 293

        #4
        Use a string, then start chopping the string in half. Then start converting the ASCII values to characters.

        For example, string 0A is 0x3061. However, you want it to be equal to 0x0A. The conversion isn't too bad.

        For each character, convert it to lowercase. Then check that character c is a digit (0 <= c-'0' <= 9-0) or alphabetic (0 <= c-'a' <= 'f'-'a'). If the character is a digit, subtract '0' from it's value. If it's alphabetic, subtract ('a'-10) from it, since A is 10 in hexadecimal.

        However, two characters will be stored into one character location, because 0-F can be stored with 4 bits, and a character is 8 bits. In that case, multiply the first character by 16 (1<<4) to shift it left four bits. Then simply add in the second character's value.

        Comment

        • candexis
          New Member
          • Aug 2006
          • 51

          #5
          Could you please help me to put that in code, so I can understant how to do it. thanks.

          Comment

          • D_C
            Contributor
            • Jun 2006
            • 293

            #6
            Actually, Banfa's way is much easier, and I think I may have been a little side tracked in my response.
            Code:
            char data[4][4];
            char ch;
            int count;
            for(count = 0; count < 16; count++)
            {
              cin >> ch;
              data[count%4][count/4] = ch;
            }

            Comment

            Working...