在我读取的文本文件中是hello world。我需要此文件将文本转换为二进制十六进制,密码,旋转。
当我执行./main -b txt.txt
时,它打印的次数比所需次数多。对于十六进制输出,其功能相同。
hello world的haxe十进制输出将输出
48656c6c6f20576f726c640a6c6c6f20576f726c640a6c6c6f20576f726cz1882855 二进制输出为:1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 1010 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1 100100 1010 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100
如果我将1根Ceaser密码打印出来,则Ceaser密码也不起作用:
IfmmpUXpseYmmpUXpseYmmpUXps
您好世界
该怎么办才能解决此问题,因为它不会打印出比所需次数更多的时间。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <getopt.h>
#include <string>
#include "more.h"
using namespace std;
int main(int argc, char*argv[]){
int op = 0;
int opt;
int fd = 1;
int buff = 10;
char buffer[buff];
string s;
int n = 0;
int bytesread= 10;
int i = 2;
ssize_t nr = 0;
string v;
while((op = getopt(argc, argv, "xbs:n:c:r:")) != -1){
switch(op)
{
case 's':
buff = atoi(optarg);
i += 2;
break;
case 'n':
bytesread = atoi(optarg);
n =1;
i += 2;
break;
case 'c':
i++;
v = argv[i];
fd = open(v.c_str(),O_RDONLY);
if (n == 1){
nr = read(fd,buffer,bytesread);
if (nr == -1){
perror("reading file");
exit(1);
}
opt = atoi(optarg);
s = cipher(buffer,opt,buff);
write(STDOUT_FILENO,s.c_str(),s.size());
}
else{
do {
nr = read(fd,buffer,bytesread);
if (nr == -1){
perror("reading file");
exit(1);
}
opt = atoi(optarg);
s = cipher(buffer,opt,buff);
write(STDOUT_FILENO,s.c_str(),s.size());
}while(nr != 0);
}
break;
case 'r':
i++;
v = argv[i];
fd = open(v.c_str(),O_RDONLY);
if (n == 1){
nr = read(fd,buffer,bytesread);
opt = atoi(optarg);
s = rotation(buffer,opt);
write(STDOUT_FILENO,s.c_str(),s.size());
}
else{
do{
nr = read(fd,buffer,bytesread);
if (nr == -1){
perror("reading file");
exit(1);
}
opt = atoi(optarg);
s = rotation(buffer,opt);
write(STDOUT_FILENO,s.c_str(),s.size());
}while(nr != 0);
}
break;
case 'x':
v = argv[i];
fd = open(v.c_str(),O_RDONLY);
if (n == 1){
nr = read(fd,buffer,bytesread);
s = hexa(buffer,buff);
write(STDOUT_FILENO,s.c_str(),s.size());
}
else{
do {
nr = read(fd,buffer,bytesread);
if (nr == -1){
perror("reading file");
exit(1);
}
s = hexa(buffer,buff);
write(STDOUT_FILENO,s.c_str(),s.size());
}while(nr != 0);
}
break;
case 'b':
v = argv[i];
fd = open(v.c_str(),O_RDONLY);
if (n == 1){
nr = read(fd,buffer,bytesread);
if (nr == -1){
perror("reading file");
exit(1);
}
s = binary(buffer);
write(STDOUT_FILENO,s.c_str(),s.size());
}
else{
do{
nr = read(fd,buffer,bytesread);
if (nr == -1){
perror("reading file");
exit(1);
}
s = binary(buffer);
write(STDOUT_FILENO,s.c_str(),s.size());
}while(nr != 0);
}
break;
default:
int buf[10];
fd = open(argv[1],O_RDONLY);
if (fd == -1){
perror("opening file");
exit(1);
}
do {
nr = read(fd,buf,bytesread);
if (nr == -1){
perror("reading file");
exit(1);
}
write(1,buf,nr);
}while(nr != 0);
close(fd);
break;
}
}
return 0;
}
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string>
#include <bits/stdc++.h>
#include "more.h"
using namespace std;
constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
string cipher(char buffer[],int opt,int size){
string s = "";
for (int i = 0;i < size-1;i++){
if (isupper(buffer[i]))
s += char(int(buffer[i]+opt-65)%26+65);
else
s += char(int(buffer[i]+opt-97)%26+97);
}
return s;
}
string rotation(char buffer[], int opt){
string s = buffer;
reverse(s.begin(), s.begin()+opt);
reverse(s.begin()+opt, s.end());
reverse(s.begin(), s.end());
return s;
}
string hexa(char buffer[],int size){
string str;
string n = buffer;
int s = n.size();
for( int i = 0; i < s; i++)
{
char const byte = buffer[i];
str += hexmap[ ( byte & 0xF0 ) >> 4 ];
str += hexmap[ ( byte & 0x0F ) >> 0 ];
}
return str;
}
string binary(char buffer[]){
string n = buffer;
int s = n.size();
string t = "";
for (int i = 0; i <= s+1; i++) {
int val = int(n[i]);
string bin = "";
while (val > 0){
(val % 2)? bin.push_back('1'):
bin.push_back('0');
val /= 2;
}
reverse(bin.begin(),bin.end());
t += bin + " " ;
}
return t;
}