From 5fcee293d1432024d2e4970734d01121fdd0ce21 Mon Sep 17 00:00:00 2001 From: gwdwyy Date: Tue, 9 Jul 2013 14:01:26 +0800 Subject: [PATCH] update cppcommon --- cppcommon/str_functs.cpp | 28 ++++++++++++++++------------ cppcommon/str_functs.h | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cppcommon/str_functs.cpp b/cppcommon/str_functs.cpp index da4ef3d..9ef3b92 100644 --- a/cppcommon/str_functs.cpp +++ b/cppcommon/str_functs.cpp @@ -198,12 +198,13 @@ namespace CPPCOMMON char * utfStr = new char[len<<1]; for(int i = 0; i < len; i+=2) { - uint16_t tmp1 = uniStr[i]; - tmp1 <<= 8; - tmp1&= 0xff00; - uint16_t tmp2 = uniStr[i+1]; - tmp2 &= 0x00ff; - uniArr[i>>1] = tmp1 | tmp2; + //uint16_t tmp1 = uniStr[i]; + //tmp1 <<= 8; + //tmp1 &= 0xff00; + //uint16_t tmp2 = uniStr[i+1]; + //tmp2 &= 0x00ff; + //uniArr[i>>1] = tmp1 | tmp2; + uniArr[i>>1] = twocharToUint16(uniStr[i], uniStr[i+1]); } string res; @@ -269,11 +270,14 @@ namespace CPPCOMMON { for(uint i = 0; i < uniLen; i++) { - char c = 0; - c = ((pUni[i]>>8) & 0x00ff); - res += c; - c = (pUni[i] & 0x00ff); - res += c; + //char c = 0; + //c = ((pUni[i]>>8) & 0x00ff); + //res += c; + //c = (pUni[i] & 0x00ff); + //res += c; + pair char2= uint16ToChar2(pUni[i]); + res += char2.first; + res += char2.second; } } delete [] pUni; @@ -324,7 +328,7 @@ int main() // //cout< #include #include +#include #include #include "typedefs.h" namespace CPPCOMMON @@ -31,5 +32,20 @@ namespace CPPCOMMON string unicodeToUtf8(const string& uniStr); int utf8ToUnicode(const char* inutf8, int len, uint16_t* unicode); string utf8ToUnicode(const string& utfStr); + + inline uint16_t twocharToUint16(char high, char low) + { + return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff)); + } + + inline pair uint16ToChar2(uint16_t in) + { + pair res; + res.first = (in>>8) & 0x00ff; //high + res.second = (in) & 0x00ff; //low + return res; + } + + } #endif