urlencode 질문드립니다.

redpig의 이미지

static char _x2c(char hex_up, char hex_low) 
{
        char digit;
 
        digit = 16 * (hex_up >= 'A' 
                ? ((hex_up & 0xdf) - 'A') + 10 : (hex_up - '0'));
        digit += (hex_low >= 'A' 
                ? ((hex_low & 0xdf) - 'A') + 10 : (hex_low - '0'));
        return (digit);
}
 
char *qURLencode(char *str) 
{
        char *encstr, buf[2+1];
        unsigned char c;
        int i, j;
 
        if(str == NULL) return NULL;
        if((encstr = (char *)malloc((strlen(str) * 3) + 1)) == NULL) 
                return NULL;
 
        for(i = j = 0; str[i]; i++) 
        {
                c = (unsigned char)str[i];
                if((c >= '0') && (c <= '9')) encstr[j++] = c;
                else if((c >= 'A') && (c <= 'Z')) encstr[j++] = c;
                else if((c >= 'a') && (c <= 'z')) encstr[j++] = c;
                else if((c == '@') || (c == '.') || (c == '/') || (c == '\\')
                        || (c == '-') || (c == '_') || (c == ':') ) 
                        encstr[j++] = c;
                else 
                {
                        sprintf(buf, "%02x", c);
                        encstr[j++] = '%';
                        encstr[j++] = buf[0];
                        encstr[j++] = buf[1];
                }
        }
        encstr[j] = '\0';
 
        return encstr;
}

구글링해서 찾아낸 urlencode 소스인데요..
이 소스로 "한글"을 인코딩해보면 "%C7%D1%B1%DB" 이렇게 나오는데요..

자바스크립트의 encodeURIComponent()로 똑같이 "한글"을 인코딩해보면 "%ED%95%9C%EA%B8%80" 이렇게 나오던데요..

왜 결과값이 다른걸까요?
구글링해서 찾아본 다른 소스들도 모두 위 소스와 같은 방식으로 인코딩을 하던데요..
자바스크립트 encodeURIComponent()와 같은 인코딩 결과값이 나오게 하고 싶은데 어떻게 해야하는지 도무지 모르겠네요..

addnull의 이미지

raymundo의 이미지

그건 입력으로 들어간 "한글" 문자열의 인코딩이 뭐냐에 따라 달라진 거라서요,

"%C7%D1%B1%DB" 이렇게 나올 때는 euc-kr, 윈도우라면 cp949 로 인코딩되어 있는 거고
"%ED%95%9C%EA%B8%80" 이건 UTF-8 이네요.

좋은 하루 되세요!

addnull의 이미지

질문자께서 올리신 코드도 정상 동작하는데, 괜히 다른 코드를 링크로 남겼네요.
말씀하신 것처럼 "한글" 이라는 문자열 자체가 cp949 또는 euc-kr 일때는 "%C7%D1%B1%DB"라고 나오지만
UTF-8 로 하면 질문자께서 원하시는 결과가 나오네요 ㅎㅎ

개발자라라ㅏ의 이미지

그래서 어떻게해야 원하는 결과가 나오는건가요?

sephiron의 이미지

무엇을 원하시나요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.