Home Database Mysql Tutorial No2:luacom中文内容输出BUG及修正

No2:luacom中文内容输出BUG及修正

Jun 07, 2016 pm 03:37 PM
bug Chinese correction content output

使 用cell.Value2输出中文内容时总是乱码。怀疑是utf-8的原因,转换后结果仍然是乱码。自己再写个转换的再测试,依然是乱码,莫非有BUG!? 下个LUACOM的源码,查看函数 tLuaCOMTypeHandler::com2lua 和 tStringBuffer tUtil::bstr2string 。整个过程看起来

    使用cell.Value2输出中文内容时总是乱码。怀疑是utf-8的原因,转换后结果仍然是乱码。自己再写个转换的再测试,依然是乱码,莫非有BUG!?

下个LUACOM的源码,查看函数tLuaCOMTypeHandler::com2luatStringBuffer tUtil::bstr2string。整个过程看起来都OK,但再测试发现,结果字符串少了一个bytes。

    在tLuaCOMTypeHandler::com2lua VT_BSTR分支中返回结果恰好是减去1,将其修改

lua_pushlstring(L, str, str.getSize()-1);修改为lua_pushlstring(L, str, str.getSize());

    重新编译LUACOM,再看cell.Value2输出结果,终于正确了。由于没进行全面测试,不知道其此修改会不会引入错误。资源里有个已经编译好的。

--lc是从网上抄来的unicode utf-8 ansi相互转换的函数

package.cpath=[[C:\Program Files\Lua\5.1\clibs\?.dll;d:\loonlib\sample\lc\?.dll]]
require "luacom"
require "lc"

function print_table(t) for k,v in pairs(t) do print(k,v) end end
excel = luacom.CreateObject("Excel.Application")
excel.Visible = true
excel.Workbooks:Add();
--luacom.ViewTypeLib(excel);
sheet=excel.Sheets(1);
local r=sheet:Range("E6");

local s = "严中";
ws, s2=lc.a2w(s); --0x25 0x4e 0x2d 0x4e 0x00 0x00 6
print("unicode : " .. lc.bstr(ws, s2));
us, s2=lc.w2u(ws, s2); --0xe4 0xb8 0xa5 0xe4 0xb8 0xad 0x00 0x00 8
print("utf8 : " .. lc.bstr(us, s2));

r.Value2=us;
ws, s2=lc.u2w(r.Value2, s2);
print("unicode : " .. lc.bstr(ws, s2));
as, s2=lc.w2a(ws, s2);
print("ansi : " .. lc.bstr(as, s2));
print(as);
Copy after login

lc.def

LIBRARY	"lc"

EXPORTS
	luaopen_lc
Copy after login


lc.h

extern "C" {
#include "lua.h"
#include "lualib.h" 
#include "lauxlib.h"
int luaopen_local(lua_State* L);
}

#include <locale.h>
#include <cstring>
#ifdef WIN32
#include <windows.h>
#include <winnls.h>
#else
#include <cstdlib>
#endif


#define LN_lc "lc"
int lua_a2w(lua_State* L);
int lua_u2w(lua_State* L);
int lua_w2a(lua_State* L);
int lua_w2u(lua_State* L);
int lua_u2a(lua_State* L);
int lua_a2u(lua_State* L);
int lua_bstr(lua_State* L);

int lua_help(lua_State* L);
wchar_t* mb2wc(const char* mbstr, int& s2, int cp);
char* wc2mb(const wchar_t* wcstr, int& s2, int cp);
</cstdlib></winnls.h></windows.h></cstring></locale.h>
Copy after login


lc.cpp

#include "lc.h"
//g++ -shared -s -o lc.dll -O3 lc.cpp lc.def -llua5.1 -DWIN32 -I%loon%/lua/src -L%loon%/lib/gcc_dll/debug -Wl,--out-implib,liblc.a 

int lua_bstr(lua_State* L) {
	const char* s = luaL_optstring(L, 1, "");
	int len = luaL_optnumber(L, 2, 0);
	if (strcmp(s, "")==0 || 0==len) {
		lua_pushstring(L, s);
	} else {
		luaL_Buffer b;
		luaL_buffinit(L, &b);
		char* byte = (char*)malloc(64);
		for (int i=0; i<len sprintf char lual_addstring byte free lual_pushresult return int lua_u2w l result="0;" size_t len="0;" const mbstr="lua_tolstring(L," if>0) {
		int s2 = 0;
		wchar_t* wcstr = mb2wc(mbstr, s2, CP_UTF8);
		if (wcstr) {
			lua_pushlstring(L, (const char*)wcstr, s2);
			lua_pushnumber(L, s2);
			delete[] wcstr;
			result = 2;
		}
	}
	return result;
}

int lua_a2w(lua_State* L) {
	int result = 0;
	size_t len = 0;
	const char* mbstr = lua_tolstring(L, 1, &len);
	if (mbstr && len>0) {
		int s2 = 0;
		wchar_t* wcstr = mb2wc(mbstr, s2, CP_ACP);
		if (wcstr) {
			lua_pushlstring(L, (const char*)wcstr, s2);
			lua_pushnumber(L, s2);
			delete[] wcstr;
			result = 2;
		}
	}
	return result;
}

int lua_w2a(lua_State* L) {
	int result = 0;
	size_t len = 0;
	const char* wcstr = lua_tolstring(L, 1, &len);
	if (wcstr && len>0) {
		int s2 = 0;
		char* mbstr = wc2mb((wchar_t*)wcstr, s2, CP_ACP);
		if (mbstr) {
			lua_pushlstring(L, mbstr, s2);
			lua_pushnumber(L, s2);
			delete[] mbstr;
			result = 2;
		}
	}
	return result;
}

int lua_w2u(lua_State* L) {
	int result = 0;
	size_t len = 0;
	const char* wcstr = lua_tolstring(L, 1, &len);
	if (wcstr && len>0) {
		int s2 = 0;
		char* mbstr = wc2mb((wchar_t*)wcstr, s2, CP_UTF8);
		if (mbstr) {
			lua_pushlstring(L, mbstr, s2);
			lua_pushnumber(L, s2);
			delete[] mbstr;
			result = 2;
		}
	}
	return result;
}

int lua_u2a(lua_State* L) {
	int result = 0;
	size_t len = 0;
	const char* mbstr = lua_tolstring(L, 1, &len);
	if (mbstr && len>0) {
		int s2 = 0;
		wchar_t* wcstr = mb2wc(mbstr, s2, CP_UTF8);
		if (wcstr) {
			char* nmbstr = wc2mb(wcstr, s2, CP_ACP);
			if (nmbstr) {
				lua_pushlstring(L, nmbstr, s2);
				lua_pushnumber(L, s2);
				result = 2;
				delete[] nmbstr;
			}
			delete[] wcstr;
		}
	}
	return result;
}

int lua_a2u(lua_State* L) {
	int result = 0;
	size_t len = 0;
	const char* mbstr = lua_tolstring(L, 1, &len);
	if (mbstr && len>0) {
		int s2 = 0;
		wchar_t* wcstr = mb2wc(mbstr, s2, CP_ACP);
		if (wcstr) {
			char* nmbstr = wc2mb(wcstr, s2, CP_UTF8);
			if (nmbstr) {
				lua_pushlstring(L, nmbstr, s2);
				lua_pushnumber(L, s2);
				result = 2;
				delete[] nmbstr;
			}
			delete[] wcstr;
		}
	}
	return result;
}

wchar_t* mb2wc(const char* mbstr, int& s2, int cp) {
	wchar_t* wcstr = NULL;
#ifdef WIN32
	int size = MultiByteToWideChar(cp, 0, mbstr, -1, NULL, 0);
#else
	size_t size = mbstowcs(NULL, mbstr, 0);
#endif
	wcstr = new wchar_t[size];
	if (wcstr) {
		memset(wcstr, 0, size * sizeof(wchar_t));
#ifdef WIN32
		int ret = MultiByteToWideChar(cp, 0, mbstr, -1, wcstr, size);
		if (ret == 0) { // MultiByteToWideChar returns 0 if it does not succeed.
#else
		size_t ret = mbstowcs(wcstr, mbstr, size+1);
		if (ret == -1) {
#endif
			delete[] wcstr;
			wcstr = NULL;
		}
		s2 = 2*size;
	}
	return wcstr;
}

char* wc2mb(const wchar_t* wcstr, int& s2, int cp) {
	char* mbstr = NULL;
#ifdef WIN32
	int size = WideCharToMultiByte(cp, 0, wcstr, -1, NULL, 0, NULL, NULL);
#else
	size_t size = wcstombs(NULL, wcstr, 0);
#endif
	mbstr = new char[size];
	if (mbstr) {
		memset(mbstr, 0, size * sizeof(char));
#ifdef WIN32
		int ret = WideCharToMultiByte(cp, 0, wcstr, -1, mbstr, size, NULL, NULL);
		if (ret == 0) { // MultiByteToWideChar returns 0 if it does not succeed.
#else
		size_t ret = wcstombs(mbstr, wcstr, size+1);
		if (ret == -1) {
#endif
			delete[] mbstr;
			mbstr = NULL;
		}
		s2 = size;
	}
	return mbstr;
}

int lua_help(lua_State* L) {
	const char* s= 
		"Simple Characters Transformation\n"
		"  a2w(ansi to unicode)\n"
		"  u2w(utf8 to unicode)\n"
		"  w2a(unicode to ansi)\n"
		"  w2u(unicode to utf8)\n"
		"  u2a(utf8 to ansi)\n"
		"  a2u(ansi to utf8)\n"
		"  bstr(bytes of str)\n"
		"  help(show this)\n\n"
		"  example :\n"
		"    local s = \"I like lua\"\n"
		"    print(lc.bstr(s, string.len(s)+1))\n"
		"    local ws, s2 = lc.a2w(s)\n"
		"wunoman@qq.com 2012/03/06\n"
		;
	lua_pushstring(L, s);

	return 1;
}

luaL_reg lrg_lc[] = {
	{"a2w", lua_a2w},
	{"u2w", lua_u2w},
	{"w2a", lua_w2a},
	{"w2u", lua_w2u},
	{"u2a", lua_u2a},
	{"a2u", lua_a2u},
	{"bstr", lua_bstr},
	{"help", lua_help},
    {NULL, NULL}
};

extern "C" int luaopen_lc(lua_State* L) {
    luaL_register(L, LN_lc, lrg_lc);
	return 1;
}
</len>
Copy after login



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set Chinese in Call of Duty: Warzone mobile game How to set Chinese in Call of Duty: Warzone mobile game Mar 22, 2024 am 08:41 AM

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Setting up Chinese with VSCode: The Complete Guide Setting up Chinese with VSCode: The Complete Guide Mar 25, 2024 am 11:18 AM

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

How to set Excel table to display Chinese? Excel switching Chinese operation tutorial How to set Excel table to display Chinese? Excel switching Chinese operation tutorial Mar 14, 2024 pm 03:28 PM

Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

How to display Chinese characters correctly in PHP Dompdf How to display Chinese characters correctly in PHP Dompdf Mar 05, 2024 pm 01:03 PM

How to display Chinese characters correctly in PHPDompdf When using PHPDompdf to generate PDF files, it is a common challenge to encounter the problem of garbled Chinese characters. This is because the font library used by Dompdf by default does not contain Chinese character sets. In order to display Chinese characters correctly, we need to manually set the font of Dompdf and make sure to select a font that supports Chinese characters. Here are some specific steps and code examples to solve this problem: Step 1: Download the Chinese font file First, we need

An effective way to fix Chinese garbled characters in PHP Dompdf An effective way to fix Chinese garbled characters in PHP Dompdf Mar 05, 2024 pm 04:45 PM

Title: An effective way to repair Chinese garbled characters in PHPDompdf. When using PHPDompdf to generate PDF documents, garbled Chinese characters are a common problem. This problem usually stems from the fact that Dompdf does not support Chinese character sets by default, resulting in Chinese content not being displayed correctly. In order to solve this problem, we need to take some effective ways to fix the Chinese garbled problem of PHPDompdf. 1. Use custom font files. An effective way to solve the problem of Chinese garbled characters in Dompdf is to use

How to change the Microsoft Edge browser to open with 360 navigation - How to change the opening with 360 navigation How to change the Microsoft Edge browser to open with 360 navigation - How to change the opening with 360 navigation Mar 04, 2024 pm 01:50 PM

How to change the page that opens the Microsoft Edge browser to 360 navigation? It is actually very simple, so now I will share with you the method of changing the page that opens the Microsoft Edge browser to 360 navigation. Friends in need can take a look. I hope Can help everyone. Open the Microsoft Edge browser. We see a page like the one below. Click the three-dot icon in the upper right corner. Click "Settings." Click "On startup" in the left column of the settings page. Click on the three points shown in the picture in the right column (do not click "Open New Tab"), then click Edit and change the URL to "0" (or other meaningless numbers). Then click "Save". Next, select "

Will wwe2k24 have Chinese? Will wwe2k24 have Chinese? Mar 13, 2024 pm 04:40 PM

"WWE2K24" is a racing sports game created by Visual Concepts and was officially released on March 9, 2024. This game has been highly praised, and many players are eagerly interested in whether it will have a Chinese version. Unfortunately, so far, "WWE2K24" has not yet launched a Chinese language version. Will wwe2k24 be in Chinese? Answer: Chinese is not currently supported. The standard version of WWE2K24 in the Steam Chinese region is priced at 199 yuan, the deluxe version is 329 yuan, and the commemorative edition is 395 yuan. The game has relatively high configuration requirements, and there are certain standards in terms of processor, graphics card, or running memory. Official recommended configuration and minimum configuration introduction:

Tips for solving Chinese garbled characters when writing txt files with PHP Tips for solving Chinese garbled characters when writing txt files with PHP Mar 27, 2024 pm 01:18 PM

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

See all articles