|

楼主 |
发表于 2004-4-6 21:53:51
|
显示全部楼层
这个问题已经解决了
原来rtsp使用的编码是utf8,而http使用的好像是本地编码。
代码如下:
- #include<stdio.h>
- #include<stdlib.h>
- #include<iconv.h>
- #define MAX_LEN 4096
- int decode_rtsp_url(char *from, char* to, char* locale, size_t len);
- char * decode_http_url(char * url);
- int
- main(int argc, char **argv)
- {
- char dest[MAX_LEN];
- if(!strcmp("rtsp",argv[1]) || !strcmp("mms", argv[1]))
- {
- // printf("decode_rtsp\n");
- int errno = decode_rtsp_url(argv[2], dest, "GB18030", MAX_LEN);
- if(-1 == errno)
- // puts("error occured when converting\n");
- return 1;
- }
- else if(!strcmp("http", argv[2]))
- {
- strcpy(dest, argv[2]);
- decode_http_url(dest);
- }
- else
- {
- printf("you should specify the protocol\n");
- return 1;
- }
- printf("%s", dest);
- return 0;
- }
- int
- decode_rtsp_url(char* from_str, char* to_str, char* locale, size_t len)
- {
- int from_len = strlen(from_str);
- len = (from_len < len)? from_len: len;
- char *str = (char*)malloc(sizeof(char) * from_len);
- char* str_free = str;
- int i,j=0;
- for(i = 0; i < len; i++)
- {
- if('%' == from_str[i])
- {
- char word[3];
- int code;
- word[0] = from_str[++i];
- word[1] = from_str[++i];
- word[3] = '\0';
- sscanf(word, "%x", &code);
- str[j++] = code;
- }
- else
- str[j++] = from_str[i];
- }
- str[j]='\0';
- //printf("utf8 encoded string str = %s\n", str);
- iconv_t utf8_locale = iconv_open(locale,"UTF8");
- size_t inputleft = len, outputleft = MAX_LEN;
- char* str_dest = to_str;
- size_t bytes = iconv(utf8_locale, &str, &inputleft,
- &str_dest, &outputleft);
- free(str_free);
- iconv_close(utf8_locale);
- return bytes;
- }
- char*
- decode_http_url(char* str)
- {
- //the converted string is stored in str.
- //So you may need to copy the orign string to another place
- //before call this function.
- char buf[MAX_LEN];
- int i,j=0;
- char word[3];
- char de;
- if(str!=NULL)
- {
- strcpy(buf,str);
- //printf("url code%s\n",buf);
- for(i=0;i<(int)strlen(buf);i++){
- if(buf[i]=='%')
- {
- word[0]=buf[i+1];word[1]=buf[i+2];word[2]='\0'; i+=2;
- sscanf(word,"%x",&de);
- //printf("%c\n",de);
- //printf("%x\n",de);
- //printf("%d\n",de);
- sprintf(str+j,"%c",de);
- j++;
- }
- else if (buf[i]=='\0') break;
- else if (buf[i]=='+'){sprintf(str+j,"%c",' ');j++;}
- else
- {
- sprintf(str+j,"%c",buf[i]);j++;
- }
- }
- str[j]='\0';
- }
- return str;
- }
复制代码
后面的那个decode_http_url就是照抄undefined的。
使用方法,假设编译出的可执行文件名为decode_url
decode_url http|mms|http url |
|