I am having a slight problem implementing this code. What I need to do is take a list of 16 unsigned characters and permute the bits according to IPTable. 0 in IPTable refers to bit 0, 1 refers to bit 1, etc. Here is my code which isn't working.

unsigned char IPTable[128] = {0, 32, 64, 96, 1, 33, 65, 97, 2, 34, 66, 98, 3, 35, 67, 99, 4, 36, 68, 100, 5, 37, 69, 101, 6, 38, 70, 102, 7, 39, 71, 103, 8, 40, 72, 104, 9, 41, 73, 105, 10, 42, 74, 106, 11, 43, 75, 107, 12, 44, 76, 108, 13, 45, 77, 109, 14, 46, 78, 110, 15, 47, 79, 111, 16, 48, 80, 112, 17, 49, 81, 113, 18, 50, 82, 114, 19, 51, 83, 115, 20, 52, 84, 116, 21, 53, 85, 117, 22, 54, 86, 118, 23, 55, 87, 119, 24, 56, 88, 120, 25, 57, 89, 121, 26, 58, 90, 122, 27, 59, 91, 123, 28, 60, 92, 124, 29, 61, 93, 125, 30, 62, 94, 126, 31, 63, 95, 127};

void IP(unsigned char * input, unsigned char * output)
{
	unsigned char i;
	for (i = 0; i < 128; i++)
	{
		output[i / 8] ^= (128 >> (IPTable[i] % 8)) & input[IPTable[i]];
	}

Thanks for the help. By the way this code definitely won't work if output isn't set to all zeroes, I already know that and that isn't the error.

Edited 7 Years Ago by kolosick.m188: n/a

Expected input and output would be helpful. Providing a small snippet of a simple test, minimal but complete and compilable, would too.

"This code doesn't do what I want (and I'm not going to tell you what that is)" isn't very helpful.

I have no idea how this compares to what you are trying to get.

#include <stdio.h>

unsigned char IPTable[128] = {0, 32, 64, 96, 1, 33, 65, 97, 2, 34, 66, 98, 3, 35, 67, 99, 4, 36, 68, 100, 5, 37, 69, 101, 6, 38, 70, 102, 7, 39, 71, 103, 8, 40, 72, 104, 9, 41, 73, 105, 10, 42, 74, 106, 11, 43, 75, 107, 12, 44, 76, 108, 13, 45, 77, 109, 14, 46, 78, 110, 15, 47, 79, 111, 16, 48, 80, 112, 17, 49, 81, 113, 18, 50, 82, 114, 19, 51, 83, 115, 20, 52, 84, 116, 21, 53, 85, 117, 22, 54, 86, 118, 23, 55, 87, 119, 24, 56, 88, 120, 25, 57, 89, 121, 26, 58, 90, 122, 27, 59, 91, 123, 28, 60, 92, 124, 29, 61, 93, 125, 30, 62, 94, 126, 31, 63, 95, 127};

void IP(unsigned char * input, unsigned char * output)
{
   unsigned char i;
   for ( i = 0; i < 128; i++ )
   {
      output[i / 8] ^= (128 >> (IPTable[i] % 8)) & input[IPTable[i]];
   }
}

int main(void)
{
   unsigned char in[128], out[128];
   int i;
   for ( i = 0; i < 128; ++i )
   {
      in[i] = i;
   }
   IP(in, out);
   for ( i = 0; i < 128; ++i )
   {
      printf("in[%3d] = 0x%02X, out[%3d] = 0x%02X\n", 
             i, (unsigned)in[i], i, (unsigned)out[i]);
   }
   return 0;
}

/* my output
in[  0] = 0x00, out[  0] = 0xD8
in[  1] = 0x01, out[  1] = 0x40
in[  2] = 0x02, out[  2] = 0x3E
in[  3] = 0x03, out[  3] = 0x00
in[  4] = 0x04, out[  4] = 0x2D
in[  5] = 0x05, out[  5] = 0x47
.
.
.
*/

Edited 7 Years Ago by Dave Sinkula: n/a

This question has already been answered. Start a new discussion instead.