My Project
sjis2euc.cc
Go to the documentation of this file.
1/* sjis2euc.cc
2 */
3
4#include "osl/misc/sjis2euc.h"
5#include <cctype>
6#include <cassert>
7#include <iostream>
8
9std::string osl::misc::sjis2euc(const std::string& str)
10{
11 if (str.empty())
12 return str;
13
14 std::string result;
15 result.reserve(str.size());
16 size_t index = 0;
17 while (index < str.size())
18 {
19 unsigned char c1 = str[index++];
20 if (0xa1 <= c1 && c1 <= 0xdf) // ignore hankaku-kana
21 continue;
22 if (isascii(c1))
23 {
24 result.push_back(c1);
25 continue;
26 }
27
28 assert(index < str.size());
29 if (index >= str.size())
30 break;
31 unsigned char c2 = str[index++];
32 sjis2euc(c1, c2);
33 result.push_back(c1);
34 result.push_back(c2);
35 }
36 return result;
37}
38
43void osl::misc::sjis2euc(unsigned char& c1, unsigned char& c2)
44{
45 if( c2 < 0x9f )
46 {
47 if( c1 < 0xa0 )
48 {
49 c1 -= 0x81;
50 c1 *= 2;
51 c1 += 0xa1;
52 }
53 else
54 {
55 c1 -= 0xe0;
56 c1 *= 2;
57 c1 += 0xdf;
58 }
59 if( c2 > 0x7f )
60 -- c2;
61 c2 += 0x61;
62 }
63 else
64 {
65 if( c1 < 0xa0 )
66 {
67 c1 -= 0x81;
68 c1 *= 2;
69 c1 += 0xa2;
70 }
71 else
72 {
73 c1 -= 0xe0;
74 c1 *= 2;
75 c1 += 0xe0;
76 }
77 c2 += 2;
78 }
79}
80// ;;; Local Variables:
81// ;;; mode:c++
82// ;;; c-basic-offset:2
83// ;;; End:
std::string sjis2euc(const std::string &str)
Convert character encoding from Shift_JIS to EUC-JP.
Definition sjis2euc.cc:9