自己写unicode转换ascii码,wchar*到char*
对于ascii码的char事实上就是unicode码wchar的首个字节码,
如wchar[20] = "qqqq"; 在内存中排码事实上是char的'q' '\0'这类。因此我们假设自己写unicode码转换为ascii的char,仅仅须要取其首字节就可以,例如以下本人写了一个wchar到char的转换的函数。
因为代码简单,加上了内存泄露測试方式。
#include <stdio.h>
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif #define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <tchar.h>
#include <Windows.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif char* UnicodeToMultibyte(WCHAR *wStr)
{ if (NULL != wStr)
{
int nLen = wcslen(wStr);
char *pStr = new char[nLen + 1];
int i = 0;
for (; i < nLen; ++i)
{
(pStr)[i] = (char)*(wStr + i);
}
(pStr)[i] = '\0';
printf("%s\n", pStr);
return pStr;
}
return NULL;
}
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); /*char *str = new char[100];*/
WCHAR wStr[250] = L"qqqqqqqqqqqqq";
char szTemp[250] = _T("wwwwwwwwwwwww");
char* pTemp = UnicodeToMultibyte(wStr);
strcpy(szTemp, pTemp);
delete pTemp; //注意,小心内存泄露
printf("%s\n", szTemp); return 0; }
当然也能够通过windows的转换函数,这个msdn上有具体解释就不解释啦
char* ConvertLPWSTRToLPSTR (LPWSTR lpwszStrIn)
{
LPSTR pszOut = NULL;
if (lpwszStrIn != NULL)
{
int nInputStrLen = wcslen (lpwszStrIn); // Double NULL Termination
int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
pszOut = new char [nOutputStrLen]; if (pszOut)
{
memset (pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
}
}
return pszOut;
}
自己写unicode转换ascii码,wchar*到char*的更多相关文章
- 创建文件夹并解决解决unicode和ASCII码转换的问题
# -*- coding: UTF-8 -*-import sysimport timeimport os #解决unicode和ASCII码转换的问题reload(sys) #解决unicode和A ...
- js 中文汉字转Unicode、Unicode转中文汉字、ASCII转换Unicode、Unicode转换ASCII、中文转换&#XXX函数代码
最近看不少在线工具里面都有一些编码转换的代码,很多情况下我们都用得到,这里脚本之家小编就跟大家分享一下这些资料 Unicode介绍 Unicode(统一码.万国码.单一码)是一种在计算机上使用的字符编 ...
- c# Unicode 转换 ASCII
/// <summary> /// Unicode 转换 ASCII /// </summary> /// <param name="theText" ...
- python Unicode转ascii码的一种方法
缘起 看到这样的数据:Marek Čech.Beniardá怎样变成相对应的ascii码呢 解决 import unicodedata s = u"Marek Čech" #(u表 ...
- [Python]Unicode转ascii码的一个好方法
写这篇文章的是一位外国人,他遇到了什么问题呢?比如有一个 Unicode 字符串他需要转为 ascii码: >>> title = u"Klüft skräms inför ...
- C语言字符转换ASCII码
//函 数 名:CharToHex()//功能描述:把ASCII字符转换为16进制//函数说明://调用函数://全局变量://输 入:ASCII字符//返 回:16进制///////// ...
- V++ MFC CEdit输出数组 UNICODE TO ASCII码
MFC怎么在静态编辑框中输出数组 //字符转ASCII码void CUTF8Dlg::OnBnClickedButtonCharAscii(){ // TODO: 在此添加控件通知处理程序代码 Upd ...
- python转换ascii码
字符转数字 ord("A") 数字转字符 chr(65)
- [PHP] chr和ord函数实现字符串和ASCII码互转
chr和ord函数是用来字符串和ASCII码互转的. ASCII码是计算机所能显示字符的编码,它的取值范围是0-255,其中包括标点.字母.数字.汉字等.在编程过程中,经常把指定的字符转化为ASCI ...
随机推荐
- [HNOI2008] GT考试(DP+矩阵快速幂+KMP)
题目链接:https://www.luogu.org/problemnew/show/P3193#sub 题目描述 阿申准备报名参加 GT 考试,准考证号为 N 位数 X1,X2…Xn(0 <= ...
- MySQL优化-存储引擎
MySQL优化-存储引擎 标签(空格分隔): mysql 存储引擎 查看存储引擎 show engines Myisam: 表锁 全文索引 Innodb: 行锁 事物 外键 Memory: 内存存储引 ...
- tensorflow 问题库
1.module 'tensorflow.python.ops.nn' has no attribute 'rnn_cell' 将tf.nn.rnn_cell ->tf.contrib.rnn
- windows或linux安装python
一.windows安装 先进入 python 官网:https://www.python.org/downloads/windows/ 选择合适的版本下载: 下载完成,双击运行安装[勾选Add to ...
- mongodb 的查询深入剖析
db.表名.find({goods_id:3}); //查询出 goods_id 为 3 的数据 db.表名.find({cat_i ...
- bzoj1270 BeijingWc2008 雷涛的小猫 DP
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1270 比较水的一道dp f1[i]为高度为i的时候的最大值 f2[i]为当前高度在第i棵树 ...
- shell清除日志小脚本
#!/bin/bash #清除日志脚本 LOG_DIR=/var/log ROOT_UID=0 #用户id为0的 ,即为root if [ "$UID" -ne "$RO ...
- Spring-statemachine fork一个region后不能进入join状态的问题
Spring-statemachine版本:当前最新的1.2.3.RELEASE版本 发现fork多个Region时,子状态全部完成后能够进入join状态.但是如果fork一个Region时Regio ...
- resin后台输出中文乱码的解决的方法!
近期从tomcat移植到resin,发现这东西不错啊! 仅仅是后台输出时有时候中文会乱码. 如今找到resin后台输出中文乱码的解决的方法: 编辑conf/resin.con文件: <!--ja ...
- xml Data Type Methods in sql server
nodes() Method (xml Data Type) https://docs.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data- ...