1

The code below shown is in visual c++

array<Byte>^ b = gcnew array <Byte> (filesize);
fs->Read(b,0,b->Length);
unsigned char *pb;
pb=(byte*)malloc(b->Length);    //pb is unmanaged here.

for(int i=0;i<b->Length;i++)
{
     *(pb+i)=InverseByte(b+i);
}

I want to call the function below to reverse each byte. How can I do this? I want to do inverse of each byte of managed array b, and put it in the unmanaged array b.

unsigned char InverseByte(unsigned char* PbByte)
{
    //something;
}
5
  • So do you want to flip the bits in each byte, rearrange the bits in each byte, or rearrange the bytes in the array? Commented May 15, 2012 at 6:20
  • if a byte is 11110010, then it should be 01001111 Commented May 15, 2012 at 9:02
  • OK, but the question in your message body doesn't have anything to do with the question in the title. Commented May 15, 2012 at 9:16
  • My question is how to send pointer to array. I tried InverseByte(&b[0]). But it did not work. Commented May 16, 2012 at 10:25
  • How did it not work? I'm still not sure what the problem is. Do you have a situation where you absolutely need to have the address of a managed array element? This is not such a situation. Commented May 16, 2012 at 12:12

4 Answers 4

3

Fix the declaration of InverseByte:

unsigned char InverseByte(unsigned char value)

So you can then use it like this:

for (int i=0; i < b->Length; i++)
{
    pb[i] = InverseByte(b[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You mean bitwise negation I presume.

unsigned char InverseByte(unsigned char c)
{
    return ~c;
}

Note that I changed the parameter to pass by value rather than passing a pointer to the value.

I also fail to see why you are using pointer arithmetic instead of indexing. That just makes your code harder to read. The loop should be written like this:

for(int i=0;i<b->Length;i++)
{      
    pb[i] = InverseByte(b[i]);
}

Comments

1
unsigned char InverseByte(unsigned char c)
{
    return (c>>7)|((c&64)>>5)|((c&32)>>3)|((c&16)>>1)|((c&8)<<1)|((c&4)<<3)|((c&2)<<5)|((c&1)<<7);
}

Comments

1

From the comments it's clear you are reversing the bytes (i.e. reordering them front-to-back) rather than inverting (i.e. subtracting a variable from its maximum value) or negating (i.e. flipping 1's and 0's), so should name the function accordingly.

Here's a neat little snippet from Seander's bithacks:

   unsigned int v;     // input bits to be reversed
   unsigned int r = v; // r will be reversed bits of v; first get LSB of v
   int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end

   for (v >>= 1; v; v >>= 1)
   {   
       r <<= 1;
       r |= v & 1;
       s--;
   }

   r <<= s; // shift when v's highest bits are zero
   return r;

If you combine Hans Passant's answer with this, you should have all the pieces to put together your function.

Comments

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.