ASCII to UNICODE UTF8 char String 等字符处理的一个小结

今天把ASCII到UNICODE的转换工作完成了

把用到的资源整理一下,说不上还有用

经常见到的问题实际上就是转换和显示,首先解决的是转换的问题

 

 

#include <string> //使用C++标准库的string类时

using namespace std; //同上

#include <sstream>

#include <iostream>

#include <stdlib.h> //要将string类和int类型直接转换最好有这些包含,

//因为自己写一个转换函数比较方便,函数定义参考如下

string getstring ( const int n )

{

std::stringstream newstr;

newstr<<n;

return newstr.str();

}

string 转 CString

CString.format(”%s”, string.c_str());

char 转 CString

CString.format(”%s”, char*);

char 转 string

string s(char *);

string 转 char *

char *p = string.c_str();

CString 转 string

string s(CString.GetBuffer());

1,string -> CString

CString.format(”%s”, string.c_str());

用c_str()确实比data()要好.

2,char -> string

string s(char *);

只能初始化,在不是初始化的地方最好还是用assign().

3,CString -> string

string s(CString.GetBuffer());

GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.

《C++标准函数库》中说的

有三个函数可以将字符串的内容转换为字符数组和C—string

1.data(),返回没有”/0“的字符串数组

2,c_str(),返回有”/0“的字符串数组

3,copy()

—————————————————————

CString与int、char*、char[100]之间的转换- -

CString与int、char*、char[100]之间的转换- -

CString互转int

将字符转换为整数,可以使用atoi、_atoi64或atol。

而将数字转换为CString变量,可以使用CString的Format函数。如

CString s;

int i = 64;

s.Format(”%d”, i)

Format函数的功能很强,值得你研究一下。

void CStrDlg::OnButton1()

{

// TODO: Add your control notification handler code here

CString

ss=”1212.12″;

int temp=atoi(ss);

CString aa;

aa.Format(”%d”,temp);

AfxMessageBox(”var is ” + aa);

}

sart.Format(”%s”,buf);

CString互转char*

///char * TO cstring

CString strtest;

char * charpoint;

charpoint=”give string a value”;

strtest=charpoint;

///cstring TO char *

charpoint=strtest.GetBuffer(strtest.GetLength());

标准C里没有string,char *==char []==string

可以用CString.Format(”%s”,char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。

CString转换 char[100]

char a[100];

CString str(”aaaaaa”);

strncpy(a,(LPCTSTR)str,sizeof(a));

//-----------------------------------------------------------------------------------------

然后是显示的问题:

cout < <(LPCTSTR)name

还有在有的时候也要用到wcount,

 

重点关注下 char* to CString

(1) char*转换成CString

  若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:

         char chArray[] = "Char  test";
         TCHAR * p = _T("Char  test");( 或LPTSTR p = _T("Char  test");)
         CString theString = chArray;
         theString.Format(_T("%s"), chArray);
         theString = p;

(2) CString转换成char*

  若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:

  方法一,使用强制转换。例如:

       CString theString( (_T("Char test "));
        LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

  方法二,使用strcpy。例如:

       CString theString( (_T("Char test "));
       LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
        _tcscpy(lpsz, theString);

  需要说明的是,strcpy(或可移值的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。

  方法三,使用CString::GetBuffer。

        如果你需要修改 CString 中的内容,它有一个特殊的方法可以使用,那就是 GetBuffer,它的作用是返回一个可写的缓冲指针。 如果你只是打算修改字符或者截短字符串,例如:
       CString s(_T("Char test "));
        LPTSTR p = s.GetBuffer(); 

        LPTSTR dot = strchr(p, ''.'');

         // 在这里添加使用p的代码

          if(p != NULL) 

         *p = _T('');
         s.ReleaseBuffer();                     // 使用完后及时释放,以便能使用其它的CString成员函数

         在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要操作的这个缓冲的 CString 对象的任何方法。因为 ReleaseBuffer 被调用之前,该 CString 对象的完整性得不到保障。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 WideCharToMultiByte 函数将 Unicode 编码的字符串转换为 ASCII 编码的字符串。UTF-8 是 Unicode 的一种编码方式,因此可以将 Unicode 编码的字符串转换为 UTF-8 编码的字符串,然后再将 UTF-8 编码的字符串转换为 ASCII 编码的字符串。 以下是一个示例代码: ```c #include <stdio.h> #include <windows.h> int main() { WCHAR unicodeStr[] = L"这是一个Unicode编码的字符串"; int unicodeLen = wcslen(unicodeStr); int utf8Len = WideCharToMultiByte(CP_UTF8, 0, unicodeStr, unicodeLen, NULL, 0, NULL, NULL); char* utf8Str = (char*)malloc(utf8Len + 1); WideCharToMultiByte(CP_UTF8, 0, unicodeStr, unicodeLen, utf8Str, utf8Len, NULL, NULL); utf8Str[utf8Len] = '\0'; int asciiLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)utf8Str, -1, NULL, 0, NULL, NULL); char* asciiStr = (char*)malloc(asciiLen + 1); WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)utf8Str, -1, asciiStr, asciiLen, NULL, NULL); asciiStr[asciiLen] = '\0'; printf("Unicode string: %ls\n", unicodeStr); printf("UTF-8 string: %s\n", utf8Str); printf("ASCII string: %s\n", asciiStr); free(utf8Str); free(asciiStr); return 0; } ``` 在示例代码中,首先将 Unicode 编码的字符串转换为 UTF-8 编码的字符串,然后再将 UTF-8 编码的字符串转换为 ASCII 编码的字符串。注意,必须在最后一个参数传入 NULL,以便 WideCharToMultiByte 函数自动计算缓冲区的大小。还需要注意,ASCII 编码是单字节编码,因此需要在 WideCharToMultiByte 函数的第一个参数传入 CP_ACP,表示使用当前系统的 ANSI 代码页进行编码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值