XORing char * Arrays

时间:2014-01-10 20:47:16

标签: c++ arrays char xor

嗨,大家好,我的坏英语^^ ...我试图xor-some char*数组,如:

"hello" ^ "moin" ^ "servus" = xorUnit 

并将其缩回原始char*数组,如:

"hello" ^ "moin" ^ xorUnit = "servus"

但我得到的结果是“serv”而不是“servus”,这似乎是一个长度问题,希望你能帮助我:))

#include <iostream> 
#include <iomanip> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <vector> 

using namespace std; 

struct UNIT 
{ 
    int size; 
    char *content; 
}; 

 UNIT XOR( vector<UNIT> list ) 
{ 
   char* unit; 
   char* temp; 

   UNIT xorUnit;   
   int unitLength = 0; 
   int maxLength = 0; 
   int minLength = 0; 
   int bigger = 0; 

   for( int i = 0; i < list.size(); i++ ) 
   { 
     unitLength = list[i].size; 

     if( minLength == 0 && maxLength == 0 ) 
     { 
       maxLength = unitLength; 
     } 
     else if( unitLength > maxLength ) 
     { 
       minLength = maxLength; 
       maxLength = unitLength; 
       bigger = 1; 
     } 
     else if( unitLength < maxLength ) 
     { 
       minLength = unitLength; 
       bigger = 0; 
     } 
     printf("bigger: %d\n", bigger); 
     printf("MaxLänge: %d\n", maxLength); 
     printf("MinLänge: %d\n", minLength);   

     temp = new char[maxLength]; 

     for(int j = 0; j < minLength; j++) 
     { 
       temp[j] = list[i].content[j] ^ unit[j]; 
     } 
     for(int j = minLength; j < maxLength; j++) 
     { 
       temp[j] = list[i].content[j]; 
     } 
     unit = temp; 
   } 

   xorUnit.content = unit; 
   if( bigger == 0 ) { 
     xorUnit.size = minLength; 
   } else { 
     xorUnit.size = maxLength; 
   } 
   bigger = 0; 

   return xorUnit; 
} 


int main(int argc,char **argv) {       
   int i; 

   vector<UNIT> list; 
   vector<UNIT> backupList; 

   //Eingabeunits 
   UNIT input1; 
   UNIT input2; 
   UNIT input3; 

   UNIT xorUnit;   
   UNIT output; 

   input1.size = 5; 
   input1.content = "hallo";     
   input2.size = 6; 
   input2.content = "servus";   
   input3.size = 4; 
   input3.content = "moin"; 

   list.push_back( input1 ); 
   list.push_back( input2 );   
   list.push_back( input3 );   

   for( i = 0; i < list.size(); i++ ) 
   { 
      output = list[i]; 
      printf("Vector(%d): %s %d\n", i, output.content, output.size); 
   } 

   xorUnit = XOR( list ); 
   printf("XOR: %s %d\n\n\n", xorUnit.content, xorUnit.size); 

   backupList.push_back( input3 ); 
   backupList.push_back( input1 );   
   backupList.push_back( xorUnit );   

   for( i = 0; i < backupList.size(); i++ ) 
   { 
      output = backupList[i]; 
      printf("Vector(%d): %s %d\n", i, output.content, output.size); 
   } 

   xorUnit = XOR( backupList ); 
   printf("XOR: %s %d\n", xorUnit.content, xorUnit.size); 

   return 0; 
}

1 个答案:

答案 0 :(得分:2)

因此,我完全不清楚为什么你的XOR功能如此复杂。如果我正确理解您的问题,XOR函数可以简单地写为:

UNIT XOR( vector<UNIT> list )
{
    // Find the length of the longest unit
    int maxLength = 0;
    for (int i=0; i<list.size(); ++i)
        maxLength = std::max(maxLength, list[i].size);

    // Allocate space for the new unit.
    // Note that calloc will zero out any allocated space.
    UNIT xorUnit;
    xorUnit.size = maxLength;
    xorUnit.content = (char *) calloc(maxLength + 1, sizeof(char)); 

    // xor each member of the list with xorUnit
    for (int i = 0; i<list.size(); ++i)
        for (int j=0; j<list[i].size; ++j)
            xorUnit.content[j] ^= list[i].content[j];

    return xorUnit;
}

这会产生一个输出:

Vector(0): hallo 5
Vector(1): servus 6
Vector(2): moin 4
XOR: vkwts 6


Vector(0): moin 4
Vector(1): hallo 5
Vector(2): vkwts 6
XOR: servus 6

注意:您很幸运能够获得可打印字符的示例。通常,这种类型的代码会产生许多被认为是可打印字符的字符。例如,"Hi" ^ "Ho" => "\0\9",两者都不是可打印的字符。