Project

General

Profile

Bug #4173 » tohex.cc

Alexander Pyhalov, 2013-10-02 05:48 PM

 
1
#include <iostream>       // std::cout
2
#include <fstream>       // ifstream
3
#include <string>         // std::string
4
#include <locale>         // std::locale, std::isalpha
5
#include <strings.h>         // bzero
6

    
7
template <int pos, typename Value>
8
inline char
9
value_to_hexchar(Value v) {
10
  v >>= pos * 4;
11
  v &= 0xf;
12

    
13
  if (v < 0xA)
14
    return '0' + v;
15
  else
16
    return 'A' + v - 0xA;
17
}
18

    
19

    
20
  
21
template <typename InputIterator, typename OutputIterator>
22
OutputIterator
23
copy_escape_html(InputIterator first, InputIterator last, OutputIterator dest) {
24
  while (first != last) {
25
    if (std::isalpha(*first, std::locale::classic()) ||
26
        std::isdigit(*first, std::locale::classic()) ||
27
        *first == '-') {
28
      *(dest++) = *first;
29

    
30
    } else {
31
      *(dest++) = '%';
32
      *(dest++) = value_to_hexchar<1>(*first);
33
      *(dest++) = value_to_hexchar<0>(*first);
34
    }
35

    
36
    ++first;
37
  }
38

    
39
  return dest;
40
}
41

    
42
int main(int argc, char* argv[])
43
{
44
	
45
        char buffer[1024];
46
        std::ifstream in;
47
	std::string input;
48
	
49
	setlocale(LC_ALL,"");
50
	bzero(buffer,1024);
51
        input = std::string("??i%<7???0?;????ѷZ?");
52
	std::cout.imbue(std::locale::classic());
53
        copy_escape_html(input.begin(),input.end(),buffer);
54
	std::cout<<buffer <<"\n";
55
  return 0;
56
}
(1-1/2)