顯示具有 CString 標籤的文章。 顯示所有文章
顯示具有 CString 標籤的文章。 顯示所有文章

2022年4月7日 星期四

[C++]CString字符串分割

 #include <iostream>

#include <cstring> #include <atlcomtime.h> using namespace std; //最後一個參數返回的是子字符串的數量 CString * SplitString(CString str, char split, int& iSubStrs); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failur CString str = "sssdf sfds jieuri lj122 98098"; // CString str = "sfsfsf"; CString* pStr; int iSubStrs; pStr = SplitString(str, ' ', iSubStrs); //如果子字符串的數量爲1 if (iSubStrs == 1) { //Convert CString to char char* pCh = (LPSTR)(LPCTSTR)str; printf("%s\n", pCh); } else { //輸出所有子字符串 for (int i = 0; i < iSubStrs; i++) { //Convert CString to char char* pCh = (LPSTR)(LPCTSTR)pStr[i]; printf("%s\n", pCh); } delete []pStr; } system("pause"); return nRetCode; } CString * SplitString(CString str, char split, int& iSubStrs) { int iPos = 0; //分割符位置 int iNums = 0; //分割符的總數 CString strTemp = str; CString strRight; //先計算子字符串的數量 while (iPos != -1) { iPos = strTemp.Find(split); if (iPos == -1) { break; } strRight = strTemp.Mid(iPos + 1, str.GetLength()); strTemp = strRight; iNums++; } if (iNums == 0) //沒有找到分割符 { //子字符串數就是字符串本身 iSubStrs = 1; return NULL; } //子字符串數組 iSubStrs = iNums + 1; //子串的數量 = 分割符數量 + 1 CString* pStrSplit; pStrSplit = new CString[iSubStrs]; strTemp = str; CString strLeft; for (int i = 0; i < iNums; i++) { iPos = strTemp.Find(split); //左子串 strLeft = strTemp.Left(iPos); //右子串 strRight = strTemp.Mid(iPos + 1, strTemp.GetLength()); strTemp = strRight; pStrSplit[i] = strLeft; } pStrSplit[iNums] = strTemp; return pStrSplit; }

[C++]UTF8轉wide character

 中文轉UTF8 在轉換為wide character 透過此方式轉換即可將文字轉為數字傳送出去

再透過解譯數字長度,將數字組回文字

---------------------------------------------------------------------

宣告以下資訊"

---.h

static const char trailingBytesForUTF8[256] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
};

static const unsigned int offsetsFromUTF8[6] = {
    0x00000000UL, 0x00003080UL, 0x000E2080UL,
    0x03C82080UL, 0xFA082080UL, 0x82082080UL
};

int u8_toucs(unsigned int* dest, int sz, char* src, int srcsz);
int u8_toutf8(char* dest, int sz, unsigned int* src, int srcsz);
int u8_wc_toutf8(char* dest, unsigned int ch);

-----.cpp

int u8_toucs(unsigned int* dest, int sz, char* src, int srcsz){
    unsigned int ch;
    char* src_end = src + srcsz;
    int nb;
    int i = 0;

    while (i < sz - 1) {
        nb = trailingBytesForUTF8[(unsigned char)*src];
        if (srcsz == -1) {
            if (*src == 0)
                goto done_toucs;
        }
        else {
            if (src + nb >= src_end)
                goto done_toucs;
        }
        ch = 0;
        switch (nb) {
            /* these fall through deliberately */
        case 3: ch += (unsigned char)*src++; ch <<= 6;
        case 2: ch += (unsigned char)*src++; ch <<= 6;
        case 1: ch += (unsigned char)*src++; ch <<= 6;
        case 0: ch += (unsigned char)*src++;
        }
        ch -= offsetsFromUTF8[nb];
        dest[i++] = ch;
    }
done_toucs:
    dest[i] = 0;
    return i;
}

int u8_toutf8(char* dest, int sz, unsigned int* src, int srcsz)
{
    unsigned int ch;
    int i = 0;
    char* dest_end = dest + sz;

    while (srcsz < 0 ? src[i] != 0 : i < srcsz) {
        ch = src[i];
        if (ch < 0x80) {
            if (dest >= dest_end)
                return i;
            *dest++ = (char)ch;
        }
        else if (ch < 0x800) {
            if (dest >= dest_end - 1)
                return i;
            *dest++ = (ch >> 6) | 0xC0;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        else if (ch < 0x10000) {
            if (dest >= dest_end - 2)
                return i;
            *dest++ = (ch >> 12) | 0xE0;
            *dest++ = ((ch >> 6) & 0x3F) | 0x80;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        else if (ch < 0x110000) {
            if (dest >= dest_end - 3)
                return i;
            *dest++ = (ch >> 18) | 0xF0;
            *dest++ = ((ch >> 12) & 0x3F) | 0x80;
            *dest++ = ((ch >> 6) & 0x3F) | 0x80;
            *dest++ = (ch & 0x3F) | 0x80;
        }
        i++;
    }
    if (dest < dest_end)
        *dest = '\0';
    return i;
}

-----------------------------------------------

Sample: 範例

char result_str2[100] = {};
uint32_t b_ucs[100] = {}; // plenty of space
int b_chars = 0;

char cbuffdata[100] = u8"哈摟哈!";
uint32_t utf_len2 = strlen(cbuffdata);
b_chars = u8_toucs(b_ucs, (utf_len2 + 1) * 4, cbuffdata, utf_len2);
//此動作已完成轉換
b_chars = u8_toutf8(result_str2, (utf_len2 + 1) * 4, b_ucs, utf_len2);
//此動作為轉換回UTF8


----可再搭配其他轉換方式將UTF8轉成ansi顯示
    CString csbuf;
    ConvertUTF8toANSI(result_str2, &csbuf);
//此函式連結

-----------------------------------------------------------------------

參考:https://www.cprogramming.com/tutorial/unicode.html

[C++]中文Ansi轉UTF8 互相轉換

VS預設文字為ANSI

使用Cstring 帶入

void ConvertCStringToUTF8(CString strValue,char* Result)
{
    char pData[9999] = {};
    USES_CONVERSION;
    strcpy(Result, T2A((LPTSTR)(LPCTSTR)(strValue)));

    /* 獲取轉換後長度 */
    int length = WideCharToMultiByte(CP_UTF8, 0, strValue, -1, NULL, 0, NULL, NULL);
    /* 獲取轉換後內容 */
    WideCharToMultiByte(CP_UTF8, 0, strValue, -1,
        (LPSTR)(Result), length, NULL, NULL);
}

void ConvertUTF8toANSI(char* strUTF8, CString* Result)
{
   
    /* 獲取轉換後長度 */
    int length = MultiByteToWideChar(CP_UTF8, NULL, (LPSTR)(strUTF8), -1, NULL, NULL);
    char szBuff[999] = {};
    /* 獲取轉換後內容 */
    MultiByteToWideChar(CP_UTF8, NULL, (LPSTR)(strUTF8), -1, (LPWSTR)szBuff, length);
    Result->Format(L"%s", szBuff);

}

2018年7月12日 星期四

[C++]cstring to char*

CString csbuf;
USES_CONVERSION;
char* chPath = T2A((LPTSTR)(LPCTSTR)csbuf);

測試環境 mfc  Unicode

2018年7月8日 星期日

[C++]CSTRING TCHAR 互相轉換


CString>TCHAR*的轉化可以用函數GetBuff()
!!!!!!使用前注意!!!!!!!!!  需加入 #define _CRT_SECURE_NO_WARNINGS 在程式開頭(stdafx.h)
CString csbuf;
csbuf.Format(L"cstring");
int cnt = csbuf.GetLength();
TCHAR* Msg = NULL;
Msg = new TCHAR[cnt+1];//記得+1
_tcscpy(Msg, csbuf);
delete[]Msg;//刪除動態創建
TCHAR*>CString的轉化
TCHAR szTchar[18] = L"TCHAR";
 CString  str;
 str.Format(_T("%s"),szTchar);  

2018年6月4日 星期一

[C++]UNICODE字符串轉換為ansi字符串

//UNICODE字符串轉換為ansi字符串
PTCHAR ptszText = TEXT("UNICODE字符串轉換為ansi字符串!");
int cbMultiByte = WideCharToMultiByte(CP_ACP, 0, ptszText, -1, NULL, 0, NULLNULL);
char *pszText = NULL;
pszText = new char[cbMultiByte];
WideCharToMultiByte(CP_ACP, 0, ptszText, -1, pszText, cbMultiByte, NULLNULL);
delete[] pszText;

//Cstring轉ansii
CString csText = L"UNICODE字符串轉換為ansi字符串!";
int iByte = WideCharToMultiByte(CP_ACP, 0, csText, -1, NULL, 0, NULLNULL);
char *chText = NULL;
chText = new char[iByte];
WideCharToMultiByte(CP_ACP, 0, csText, -1, chText, cbMultiByte, NULLNULL);
delete[] pszText;

測試環境:VS2015 MFC

[SQL]顯示千分位與小數顯示

  CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT style參數說明 1  (expression為 money 或 smallmoney型別): 0 : 預設,保留小數位後兩位,並四捨...