Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. #define w2c(x) ( (char) ((x) & 0x00FF) )
  5. #define is_uri_ch(x) ( ((x)>32) && ((x)<128) ) // uwaga! nie odporne na skutki uboczne  
  6.  
  7. using namespace std;
  8.  
  9. void _hex2_(wchar_t in, char* out) {
  10.     static char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  11.     int tmp; tmp = in & 0x000000FF; int first = tmp / 16; int second = tmp % 16;
  12.     out[0] = lookup[first]; out[1] = lookup[second];
  13. }
  14.  
  15. void hex(wchar_t* in, char* out, int max) {
  16.     int i=0, max2=max-3, j=0;
  17.     while(in[i]!=0) {
  18.         if (j<max2) {
  19.             if (is_uri_ch(in[i])) { out[j] = w2c(in[i]); ++j; }
  20.             else { out[j] = '%'; _hex2_(in[i], out+j+1); j+=3; }
  21.         }
  22.         ++i;
  23.     }
  24.     out[j+1] = 0;
  25. }
  26.  
  27.  
  28. int main() {
  29.     char buf[100];
  30.     wchar_t uri[] = L"gżegżółka";
  31.     hex(uri, buf, 100);
  32.     cout << buf << endl;
  33.     getchar();
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement