用换行符在 C 中编码 Url?

Encoding Url In C With Line Breaks?

我在互联网上找到了这个 url 编码器,并做了一些小改动,但是每当我做这样的事情时:

char encodedWord[100];
const char* word = "Stack\nOverflow";

urlencode(encodedWord, word);

输出将是这样的: "Stack0X8.51EE00001674P-1022Overflow" 而不是 Stack Overflow 之间的 x0A。

为什么输出那个?我假设是因为“EE0000”部分,字符到数字的转换出了问题。

如何让我的编码器对特殊字符更加友好?即“\n,\r,\r”。

函数

int urlencode(char *dest, const char *src)
{
    /* urlencode all non-alphanumeric characters in the C-string 'src'
       store result in the C-string 'dest'
       return the length of the url encoded C-string
    */
    char *d;
    int i;
    for(i=0, d=dest; src[i]; i++) {
        if(isalnum(src[i]) || isdigit(src[i])) {
            *(d++) = src[i];
        } else {
            snprintf(d, 4, "%%%02X", src[i]);
            d += 3;
        }
    }   
    *d = 0;
    return d-dest;
}

系统

Windows10个32位 Mingw32 (gcc 5.1.0)

制作文件

#OBJS specifies which files to compile as part of the project

OBJS = $(wildcard ./src/*.c)

#CC specifies which compiler we're using
CC = gcc

#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = 

#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = 

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS = -Wall -Wl,-subsystem,console -std=c99

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lws2_32 -lwininet -s -lshlwapi

#OBJ_NAME specifies the name of our executable
OBJ_NAME = project

#This is the target that compiles our executable
all : clean build

build:
    cls
    $(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)

clean:
    del -f $(OBJ_NAME).exe

urlencode 函数工作正常,问题是您如何打印输出。我正在写作

0X8.51EE00001674P-1022 is a hexadecimal floating point number, what you would expect to see from a %A printf specifier.

令我震惊的是,正确的输出 %0A 正好位于那个位置。这意味着您错误地将非常量字符串作为 printf 的第一个参数传递。不要做 printf(encodedWord);您应该改用 printf("%s", encodedWord)