cstring

ANSI编码方式转化为UTF-8方式

耗尽温柔 提交于 2019-12-18 03:43:20
说明: 记事本txt有四种编码方式,分别为:UTF-8、ANSI、Unicode和Unicode big endian,当进行写操作,创建的txt编码格式,与写入汉字的编码方式相同;如果写入的汉字是不同的编码方式,此时创建的txt中,会出现乱码,所以需要把汉字转化为同一编码方式。 本文主要介绍:把汉字编码方式,由ANSI方式转化为UTF-8方式: 一、ANSI转化为UTF-8程序: [cpp] view plain copy print ? CString ToUTF8( const wchar_t * buffer, int len) //返回类型为CString { int size = ::WideCharToMultiByte(CP_UTF8, 0, buffer, len, NULL, 0, NULL, NULL); if (size == 0) return "" ; std::string newbuffer; newbuffer.resize(size); ::WideCharToMultiByte(CP_UTF8, 0, buffer, len, const_cast < char *>(newbuffer.c_str()), size, NULL, NULL); //如需返回string类型,直接 return newbuffer TCHAR outstr[64

CString to char*

元气小坏坏 提交于 2019-12-17 19:32:55
问题 We are using the CString class throughout most of our code. However sometimes we need to convert to a char *. at the moment we have been doing this using variable.GetBuffer(0) and this seems to work ( this mainly happens when passing the Csting into a function where the function requires a char *). The function accepts this and we keep going. However we have lately become worried about how this works, and whether there is a better way to do it. The way i understand it to work is it passes a

string array with garbage character at end

不问归期 提交于 2019-12-17 06:53:49
问题 I have a char array buffer that I am using to store characters that the user will input one by one. My code below works but has a few glitches that I can't figure out: when I execute a printf to see what's in Buffer, it does fill up but I get garbage characters at the end it won't stop at 8 characters despite being declared as char Buffer[8]; Can somebody please explain to me what is going on and perhaps how I could fix this? Thanks. char Buffer[8]; //holds the byte stream int i=0; if (/*

VC常用数据类型使用转换详解

可紊 提交于 2019-12-17 02:27:41
VC常用数据类型使用转换详解 出 处:PCVC.NET 作 者:程佩君 刚接触VC编程的朋友往往对许多数据类型的转换感到迷惑不解,本文将介绍一些常用数据类型的使用。 我们先定义一些常见类型变量借以说明 int i = 100; long l = 2001; float f=300.2; double d=12345.119; char username[]="女侠程佩君"; char temp[200]; char *buf; CString str; _variant_t v1; _bstr_t v2; 一、其它数据类型转换为字符串 短整型(int) itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制 itoa(i,temp,2); ///按二进制方式转换 长整型(long) ltoa(l,temp,10); 二、从其它包含字符串的变量中获取指向该字符串的指针 CString变量 str = "2008北京奥运"; buf = (LPSTR)(LPCTSTR)str; BSTR类型的_variant_t变量 v1 = (_bstr_t)"程序员"; buf = _com_util::ConvertBSTRToString((_bstr_t)v1); 三、字符串转换为其它数据类型 strcpy(temp,"123"); 短整型(int) i

How is std::string implemented?

亡梦爱人 提交于 2019-12-17 02:11:32
问题 I am curious to know how std::string is implemented and how does it differ from c string?If the standard does not specify any implementation then any implementation with explanation would be great with how it satisfies the string requirement given by standard? 回答1: Virtually every compiler I've used provides source code for the runtime - so whether you're using GCC or MSVC or whatever, you have the capability to look at the implementation. However, a large part or all of std::string will be

VS2010/MFC编程入门之四十二(MFC常用类:CString类)

[亡魂溺海] 提交于 2019-12-17 01:22:31
上一节鸡啄米讲了 分割窗口 的有关知识,本节开始讲解 MFC 的一些常用类,先来说说CString类。 CString类简介 CString类作为MFC的常用类,当之无愧。可以这样说,只要是从事MFC开发,基本都会遇到使用CString类的场合。因为字符串的使用比较普遍,而CString类又提供了对字符串的便捷操作,所以它给MFC开发人员带来了高的开发效率,受到了开发者的欢迎。 大家使用 VS2010 的话,可能会见到CStringT,实际上它是一个操作可变长度字符串的模板类。CStringT模板类有三个实例:CString、CStringA和CStringW,它们分别提供对TCHAR、char和wchar_t字符类型的字符串的操作。char类型定义的是Ansi字符,wchar_t类型定义的是Unicode字符,而TCHAR取决于MFC工程的属性对话框中的Configuration Properties->General->Character Set属性,如果此属性为Use Multi-Byte Character Set,则TCHAR类型定义的是Ansi字符,而如果为Use Unicode Character Set,则TCHAR类型定义的是Unicode字符。 三个字符串类的操作是一样的,只是处理的字符类型不同。鸡啄米以CString类为讲解对象。 CString类的字符串操作

string, CString, char[]与ASCII的字符表示

情到浓时终转凉″ 提交于 2019-12-14 23:33:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 对于字符串的处理在C++中可谓是一个颇为棘手的问题,而像JAVA和C#这种基于托管的平台则不存在此类问题。 我们先来讨论一下memcpy和strcpy这两个方法。 void* memcpy(void *memTo, const void *memFrom, size_t size); char* strcpy(char * dest, const char * src); 这两个方法的区别主要有一下3个: 1. 复制的内容不同,strcpy只能复制字符串,而memcpy则可以复制任何的内容,例如char[],int,struct,class等。 2. 复制的方法不同,strcpy不需要指定要复制的长度,当遇到在src字符串中的“\0”(空字符)时才停止复制,因此很容易出现溢出的现象。而memcpy则是根据其第三个参数决定要复制的长度的,避免了此类问题。 3. 用途不同,通常在复制字符串时用strcpy,而在复制其他类型的数据时则一般采用memcpy。 4. 若要复制ASCII为1的SOH,在memcpy中要用\0表示,如果直接输入0,则代表0这个字符。 需要注意的是: 在发送与设备之间通讯的命令的情况下,很多时候命令会包含空字符null,这种时候就要慎用strcpy了

How to convert an std::string to C-style string [duplicate]

走远了吗. 提交于 2019-12-13 18:37:01
问题 This question already has answers here : How to convert a std::string to const char* or char*? (8 answers) Closed 3 years ago . I am programming in C++. As basic as this question is I cannot seem to find an answer for it anywhere. So here is the problem: I want to create a C-style string however I want to put an integer variable i into the string. So naturally, I used a stream: stringstream foo; foo << "blah blah blah blah... i = " << i << " blah blah... "; However, I need to somehow get a C

search array for string

浪子不回头ぞ 提交于 2019-12-13 01:35:24
问题 How would i implement this?Im trying to compare a array of c strings to a single string and if there is no match append it to the 2d array. char*mprt,uni[100][16]; mprt = &uni[0][0]; for (int s = 0;s <= 99;s++) { for (int z = 0;z <= 15;z++) { if (strcmp(mprt++, string1) != 0) { uni[s][z] = string1[z]; } } } 回答1: Ok ... from your comments I now get what you're trying to do. You'd want to make this into a function so you could feed words to it, but it should get you pointed in the right

How to elegantly initialize vector<char *> with string literal?

我的未来我决定 提交于 2019-12-12 08:46:56
问题 The problem comes from an exercise on C++ Primer 5th Edition: Write a program to assign the elements from a list of char* pointers to C-style character strings to a vector of strings. ----------------Oringinal Question------------ First I try the following somewhat direct way: vector<char *> vec = {"Hello", "World"}; vec[0][0] = 'h'; But compiling the code I get a warning: temp.cpp:11:43: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] vector<char *> vec = {