【问题标题】:Swap a chars bits and replace it with the new created char after swaps交换一个字符位并在交换后用新创建的字符替换它
【发布时间】:2014-06-27 23:05:41
【问题描述】:

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

我希望我可以将当​​前 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;
}

【问题讨论】:

  • 仅供参考 sizeof(char) 始终为 1
  • for (w = 0; w &gt;= 0; w--) - 奇怪的“循环”。对 malloc 的奇怪评论,它永远不会创建一个超过一个字符的数组。您不能分配给数组,但我真的无法从您的代码中判断该分配应该做什么。
  • 我更新了这篇文章,向你展示我是如何使用它来创建一个字符数组的。
  • 在循环中 w 应该被设置为 7,它现在​​可以正确读取了。
  • @user3586343,您似乎误解了“malloc()”零已分配的内存?但是,'malloc()' 不会将内存清零。因此,“加载”将指向具有随机内容的内存(不是一个很好的归零数组)。因此,表达式 '(1 & (load[i] >> w))' 产生的值将是不可预测的。

标签: c char swap bit


【解决方案1】:

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

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

用位操作'^',可以把1转0,把0转1,可以解决你的问题。

【讨论】:

  • 我有一个 8 位字节,我正在尝试用位 2 切换位 6。我可以将 char 转换为 8 位,执行交换,然后通过取 2^7 转换回来, 2^6, 2^5, 2^4, 2^3, 2^2, 2^1, 2^0 表示十进制 #,然后取那个 # 并取十六进制值。
  • 如果你想用bit 2切换bit 6,你应该使用:c ^= 221;//221=1101 1101
  • 如果你有位操作的问题,你可以阅读这个:en.wikipedia.org/wiki/…
  • 我想我开始明白了,例如如果我想交换位 5 和 1,我会使用 c ^= 228
  • 我为我上面的错误道歉,如果你想把第 6 位换成第 2 位,你应该使用:c ^= 34。如果你想交换第 5 位和第 1 位,你应该使用 c ^ = 17.
猜你喜欢
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 2018-01-14
相关资源
最近更新 更多