gpt4 book ai didi

c - 打印字符串给出字节 C

转载 作者:太空狗 更新时间:2023-10-29 16:05:29 24 4
gpt4 key购买 nike

<分区>

当我编写简单的代码将简单的字母序列编码为字节并再次解码时,我遇到了解码问题。一切都在完成我想要有 4 个字符的序列,但它还包括末尾的字节。这是我的代码:

char* B2T(int num) {
unsigned char temp;
char res[4];
int sw[] = { 6,4,2,0 };
char tab[4] = { 'A', 'C', 'G', 'T' };
int i = 0;
for (int i = 0; i < 4; i++) {
res[i] = tab[(num >> sw[i]) & 3];
}
printf_s("%s\n", res); //!!!!!!problem here!!!!!!!!
return res;
}

int main() {
FILE *I, *O;
char tab[5], opt;
int res, i, temp;
bool work = true;
while (work) {
printf_s("\nChoose option: decode or encode (d/e): ");
scanf_s("%c", &opt);
switch (opt) {
case 'e':
fopen_s(&I, "DNA.txt", "r");
fscanf_s(I, "%s", &tab, 5);
fopen_s(&O, "result.bin", "a");
while (feof(I) == 0) {
res = T2B(tab);
printf_s("%X ", res);
fprintf_s(O, "%X ", res);
fscanf_s(I, "%s", &tab, 5);
};
fclose(I);
fclose(O);
break;
case 'd':
fopen_s(&I, "result.bin", "r");
fscanf_s(I, "%X", &temp);
while (feof(I)==0) {
char* ress = B2T(temp);
fscanf_s(I, "%X", &temp);
}
fclose(I);
break;
}
}
return 0;
}

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