交换字符位并在交换后用新创建的字符替换它

时间:2014-05-10 05:56:18

标签: c char swap bit

我试图在char中交换两位值。我已经深入研究了这一点,我的代码在这一点上相当混乱。我有一个嵌套的for循环,一个遍历一个chars数组,查看每个char,将一个char的位放入一个数组,执行一些交换,最后设置当前的char(char数组中的位置) int数组。

我希望我可以将当​​前的char(在char数组中)更新为int数组位值,从而生成新的char。

基本上,目标是在char中交换位并生成新的char。

我声明的变量......

unsigned char s = 's';
FILE *fp;
char ch;
int count = 0;

//used in a loop to create an array of chars
unsigned char *load = malloc(sizeof(char)); 
int i = 0;
char temp, temp2;
int temp3, temp4;
int w, v;
int array[7];

v = 0;
for (i = 0; i < count; ++i)
   {
   for (w = 7; w >= 0; w--)
      {
      array[v] = (1 & (load[i] >> w));
      }

   temp3 = array[5];
   array[5] = array[1];
   array[1] = temp3;

   temp4 = array[0];
   array[0] = array[4];
   array[4] = temp4;

   //here I'm trying to set a char array to an int array.
   //would static casting the int array to char work?  
   //I can't find a way to convert it.
   load[i] = array;
   }

我试图为无符号字符数组中的每个字节执行此交换。

感谢您的时间,我们深表感谢。

编辑:发布整段代码

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char* argv[]) {

//unsigned char *s = "S";
unsigned char s = 's';
FILE *fp;
char ch;
int count = 0;
unsigned char *load = malloc(sizeof(char));
int i = 0;
char temp, temp2;
int temp3, temp4;
int w, v;
int array[7];

v = 0;

fp = fopen("file", "r");

if (fp == NULL)
{   
    perror("Exiting, an error occured\n");
    exit(EXIT_FAILURE); 
}

//print encrypted file

while ((ch = fgetc(fp)) != EOF)
{
    printf("%c", ch);
    ++count;
}


fclose(fp);

printf("\n\n");

fp = fopen("file", "r");

if (fp == NULL)
{
    perror("exiting\n");
    exit(EXIT_FAILURE);
}

i = 0;

//load encrypted file into usigned char array   
while ((ch = fgetc(fp)) != EOF)
{
    load[i++] = ch;
}

load[i] = 0;
fclose(fp);

//print unsigned char array in hex
for (i = 0; i < count; ++i)
{
    printf("%x ", load[i]);
}   

printf("\nNext\n");

//print unsigned char array
for (i = 0; i < count; ++i)
{
    printf("%c ", load[i]);
}       
printf("\n\n"); 


//byte swaps through every 4th byte in the unsignged char array until end   
for (i = 3; i < count;)
{
    temp = load[i-1];
    load[i-1] = load[i];
    load[i] = temp;
    i = i + 4;
}

//print new unsigned char array
for (i = 0; i < count; ++i)
{
    printf("%c ", load[i]);
}       
printf("\n\n"); 

//bit swaps on every byte in the unsigned char array
//not working
for (i = 0; i < count; ++i)
{
    for (w = 7; w >= 0; w--)
    {
        array[v] = (1 & (load[i] >> w));
    }

    temp3 = array[5];
    array[5] = array[1];
    array[1] = temp3;

    temp4 = array[0];
    array[0] = array[4];
    array[4] = temp4;

    load[i] = array;

}

//XOR every 4th byte in the unsigned char array
for (i = 3; i < count;)
{
    temp = (load[i-1] ^ s);
    temp2 = (load[i] ^ s);
    load[i-1] = temp;
    load[i] = temp2;
    i = i + 4;
}       

//print the decrypted message
for (i = 0; i < count; ++i)
{
    printf("%c ", load[i]);
}       
printf("\n\n"); 

return 0;
}

1 个答案:

答案 0 :(得分:0)

如果你想交换位。你可以试试这个(用第二位交换第一位):

int i=13; //13=1101
c ^= 3;   //3=0011,c=1110

使用位操作'^',您可以将1转为0,将0转为1,这可以解决您的问题。