Chinaunix

标题: 字符集问题 [打印本页]

作者: together007    时间: 2012-10-13 08:59
标题: 字符集问题
各位大虾:
    下面两行应该是浏览器将中文转换成别的字符集了,我如果在C程序中读到这些串后将其还原成原中文?
$%7BPRODUCT_NAME%7D
%E4%B9%A6%E6%97%97%E5%85%8D%E8%B4%B9%E5%B0%8F%E8%AF%B4
%E5%BC%80%E5%BF%83%E6%B0%B4%E6%97%8F%E7%AE%B1

      求各位指教,小弟在线等!
作者: linux_c_py_php    时间: 2012-10-13 10:04
本帖最后由 linux_c_py_php 于 2012-10-13 11:43 编辑
  1. [root@vps616 php]# php main.php
  2. ${PRODUCT_NAME}
  3. 书旗免费小说
  4. 开心水族箱[root@vps616 php]# cat main.php
  5. <?php
  6. $content = <<<EOF
  7. $%7BPRODUCT_NAME%7D
  8. %E4%B9%A6%E6%97%97%E5%85%8D%E8%B4%B9%E5%B0%8F%E8%AF%B4
  9. %E5%BC%80%E5%BF%83%E6%B0%B4%E6%97%8F%E7%AE%B1
  10. EOF;

  11. echo urldecode($content);
  12. ?>
复制代码
额, 是C代码, 写了个, 感觉有点糟烂.
  1. urldecode=0 out=${PRODUCT_NAME}书旗免费小说开心水族箱
  2. [root@vps616 c]# cat main.c
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. int urldecode(const char *in, int *in_size, char *out, int *out_size) {
  7.    if (!in || !out || !in_size || !out_size)
  8.        return -1;

  9.    int indx, ondx;

  10.    for (indx = ondx = 0; *in_size > 0 && *out_size > 0; -- *out_size, ++ ondx) {
  11.         int drain = 0;

  12.         if (in[indx] == '+') {
  13.             out[ondx] = ' ';
  14.             drain = 1;
  15.         } else if (in[indx] == '%') {
  16.             if (*in_size < 3)
  17.                 return 0;

  18.             int  i;
  19.             char base_chr;
  20.             char out_byte = 0;
  21.             
  22.             for (i = 1; i < 3; ++ i) {
  23.                 int index = indx + i;
  24.                
  25.                 if (in[index] >= '0' && in[index] <= '9')
  26.                     base_chr = '0';
  27.                 else if (in[index] >= 'a' && in[index] <= 'z')
  28.                     base_chr = 'a' - 10;
  29.                 else if (in[index] >= 'A' && in[index] <= 'Z')
  30.                     base_chr = 'A' - 10;
  31.                 else
  32.                     return -1;
  33.                 out_byte = (out_byte << 4) | (in[index] - base_chr);
  34.             }

  35.             out[ondx] = out_byte;
  36.             drain = 3;
  37.         } else {
  38.             out[ondx] = in[indx];
  39.             drain = 1;
  40.         }
  41.         
  42.         indx += drain;
  43.         *in_size -= drain;
  44.    }

  45.    return 0;
  46. }

  47. int main(int argc, char* const argv[]) {
  48.     const char *in = "$%7BPRODUCT_NAME%7D"
  49.                      "%E4%B9%A6%E6%97%97%E5%85%8D%E8%B4%B9%E5%B0%8F%E8%AF%B4"
  50.                      "%E5%BC%80%E5%BF%83%E6%B0%B4%E6%97%8F%E7%AE%B1";
  51.    
  52.     int in_size = strlen(in);
  53.     int out_size = in_size;
  54.     char *out = calloc(1, out_size + 1);
  55.     int ret = urldecode(in, &in_size, out, &out_size);
  56.     printf("urldecode=%d out=%s\n", ret, out);
  57.     free(out);
  58.     return 0;
  59. }
复制代码

作者: folklore    时间: 2012-10-13 13:34
lsv5,8741

if it is utf-8, is easy to decode...
作者: together007    时间: 2012-10-16 14:02
回复 2# linux_c_py_php


    谢谢大侠,您的操作系统用的是什么字符集?
作者: noword2k    时间: 2012-10-16 17:38
一看就是utf-8。
直接转成对应的16进制,然后decode。
作者: linux_c_py_php    时间: 2012-10-16 21:59
utf-8呀

together007 发表于 2012-10-16 14:02
回复 2# linux_c_py_php





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2