0
제목에서 알 수 있듯이 내 실수가 바이너리와 어디 있는지 알 수 없습니다. i
이 argv[0]
의 문자열 길이까지의 값을 취할 수있는 반면빈과 함께 사용하면이 코드가 충돌합니까?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char binary[360];
void printstringasbinary(char* s)
{
// A small 9 characters buffer we use to perform the conversion
char output[9];
while (*s)
{
// Convert the first character of the string to binary using itoa.
// Characters in c are just 8 bit integers, at least, in noawdays computers.
itoa(*s, output, 2);
strcat(binary, output);
++s;
}
}
int main(int argc, char *argv[])
{
int i, n;
//printf("argc: %i\n", argc);
if (argc != 3)
{
puts("Converter [Text] [hex|dec|bin]");
return 0;
}
FILE *f = fopen("Output.txt", "a");
for (i = 0; i < strlen(argv[0]); i++)
{
if (strcmp(argv[2], "hex") == 0)
fprintf(f, "%X ", (unsigned int) argv[1][i]);
else if (strcmp(argv[2], "dec") == 0)
fprintf(f, "%i ", (unsigned int) argv[1][i]);
else if (strcmp(argv[2], "bin") == 0)
for (n = 0; n < strlen(argv[1]); n++)
{
printstringasbinary(argv[1][i]);
}
fprintf(f, "%s ", binary);
}
fclose(f);
return 0;
}
코드를 게시하십시오. 게시하지 마십시오! – LPs
도움말 센터의 [how to ask] (http://stackoverflow.com/help/how-to-ask) 섹션을 방문하십시오. 질문은 완전히 자체 포함되어야합니다. – StoryTeller
'printstringasbinary (argv [1] [i]);'--->'printstringasbinary (argv [1]);' – LPs