gpt4 book ai didi

c - 交换字符位并在交换后将其替换为新创建的字符

转载 作者:行者123 更新时间:2023-11-30 17:33:57 25 4
gpt4 key购买 nike

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

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

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

我声明的变量...

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;
}

最佳答案

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

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

通过位运算“^”,可以将1转为0,将0转为1,可以解决您的问题。

关于c - 交换字符位并在交换后将其替换为新创建的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com