00001
00002
00003
00004 #include "osl/misc/sjis2euc.h"
00005 #include <cctype>
00006 #include <vector>
00007
00008 void osl::misc::sjis2euc(std::string& str)
00009 {
00010 if (str.empty()) return;
00011
00012 typedef std::vector<std::string::value_type> v_str;
00013 v_str v(str.begin(), str.end());
00014 size_t index = 0;
00015 while (index < v.size())
00016 {
00017 unsigned char c1 = static_cast<unsigned char>(v[index++]);
00018 if (!isascii(c1))
00019 {
00020 unsigned char c2 = static_cast<unsigned char>(v[index++]);
00021 sjis2euc(c1, c2);
00022 v[index-2] = c1;
00023 v[index-1] = c2;
00024 }
00025 }
00026 str.assign(v.begin(), v.end());
00027 }
00028
00033 void osl::misc::sjis2euc(unsigned char& c1, unsigned char& c2)
00034 {
00035 if( c2 < 0x9f )
00036 {
00037 if( c1 < 0xa0 )
00038 {
00039 c1 -= 0x81;
00040 c1 *= 2;
00041 c1 += 0xa1;
00042 }
00043 else
00044 {
00045 c1 -= 0xe0;
00046 c1 *= 2;
00047 c1 += 0xdf;
00048 }
00049 if( c2 > 0x7f )
00050 -- c2;
00051 c2 += 0x61;
00052 }
00053 else
00054 {
00055 if( c1 < 0xa0 )
00056 {
00057 c1 -= 0x81;
00058 c1 *= 2;
00059 c1 += 0xa2;
00060 }
00061 else
00062 {
00063 c1 -= 0xe0;
00064 c1 *= 2;
00065 c1 += 0xe0;
00066 }
00067 c2 += 2;
00068 }
00069 }
00070
00071
00072
00073