HTML+JavaScript构建一个将C/C++定义的ANSI字符串转换为MASM32定义的DWUniCode字符串的工具

36 篇文章 0 订阅
19 篇文章 0 订阅

公文一键排版系统基本完成,准备继续完善SysInfo,增加用户帐户信息,其中涉及到Win32_Account结构,其C++定义如下:

[Dynamic, Provider("CIMWin32"), UUID("{8502C4CC-5FBB-11D2-AAC1-006008C78BC7}"), AMENDMENT]
class Win32_UserAccount : Win32_Account
{
uint32 AccountType;
string Caption;
string Description;
boolean Disabled;
string Domain;
string FullName;
datetime InstallDate;
boolean LocalAccount;
boolean Lockout;
string Name;
boolean PasswordChangeable;
boolean PasswordExpires;
boolean PasswordRequired;
string SID;
uint8 SIDType;
string Status;
};

 由于WMI使用的是UniCode(WCHAR),所以我们在MASM32中要将以上成员属性AccountType、Caption……按DW UniCode(WCHAR)来定义,如下:

AccountType dw 'A','c','c','o','u','n','t','T','y','p','e', 0, 0
Caption dw 'C','a','p','t','i','o','n', 0, 0
Description dw 'D','e','s','c','r','i','p','t','i','o','n', 0, 0
Disabled dw 'D','i','s','a','b','l','e','d', 0, 0
Domain dw 'D','o','m','a','i','n', 0, 0
FullName dw 'F','u','l','l','N','a','m','e', 0, 0
InstallDate dw 'I','n','s','t','a','l','l','D','a','t','e', 0, 0
LocalAccount dw 'L','o','c','a','l','A','c','c','o','u','n','t', 0, 0
Lockout dw 'L','o','c','k','o','u','t', 0, 0
Name dw 'N','a','m','e', 0, 0
PasswordChangeable dw 'P','a','s','s','w','o','r','d','C','h','a','n','g','e','a','b','l','e', 0, 0
PasswordExpires dw 'P','a','s','s','w','o','r','d','E','x','p','i','r','e','s', 0, 0
PasswordRequired dw 'P','a','s','s','w','o','r','d','R','e','q','u','i','r','e','d', 0, 0
SID dw 'S','I','D', 0, 0
SIDType dw 'S','I','D','T','y','p','e', 0, 0
Status dw 'S','t','a','t','u','s', 0, 0

虽然MASM32 在QEDITOR 的 Conversions里提供了Text to DW UNICODE菜单项功能,但这个菜单项还不能实现自动从类中提出成员属性名称并转换为MASM32的DW UNICODE字符串的格式,所以我们还是先得手工将AccountType、Caption……提取出来。

成员少还行,成员多的话就太麻烦了。

于是用HTML+JavaScript写了一个简单的转换工具,运行效果如下:

 

完整代码如下:

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="PurpleEndurer">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>ANSI String 2 MASM32 DW UniCode String</title>
 </head>
 <body>

<table>
<caption>
	<P style="color:purple;font:18pt bold;">ANSI String 2 MASM32 DW UniCode String&nbsp;&nbsp;<input type="button" value="转换" onclick="tran()"></P>
</caption>

<tr>
	<td>
		<P align="center">ANSI 字符串</P>
	</td>
	<td>
		<P align="center">MASM32 DW UniCode String</P>
	</td>
</tr>
<tr>
	<td>
 	<textarea id="taAnsi" rows="50" cols="40" align="left">
uint32 AccountType;
string Caption;
string Description;
boolean Disabled;
string Domain;
string FullName;
datetime InstallDate;
boolean LocalAccount;
boolean Lockout;
string Name;
boolean PasswordChangeable;
boolean PasswordExpires;
boolean PasswordRequired;
string SID;
uint8 SIDType;
string Status;		
	</textarea>     		
	</td>
	<td>
		<textarea id="taUni" rows="50" cols="90"></textarea>
	</td>
</tr>
</table>

<script>


//功能:删除字符串中的所有空格
//记录:20230726创建
String.prototype.eliminateSpace = function()
{
	return this.replace(/\s*/g,"");
}

//去除首尾空格
String.prototype.trim = function() 
{
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
	/*var t = this.replace(/(^\s*)|(\s*$)/g, ""); 
	return t =t.replace(/(^&nbsp;*)|(&nbsp*$)/g, ""); */
} 


	//功能:将ANSI字符串转换成MASM32 Unicode字符串
	//记录:20230811建
	//输入:s=ANSI字符串
	//输出:MASM32 DW Unicode字符串
	function ansiStr2UniStr(s)
	{
		var r = s.split('');
		//document.write(r+"<br>"); 
		return " dw '" + r.join("','") + "', 0, 0";
	}//ansiStr2UniStr(s)
	

function getItem(a)
{
	var s = a.split(' ');
	var j = 0;
	while (j < s.length)
	{
		if (''==s[j].eliminateSpace())
		{
			s.pop();
		}
		else
		{
			j++;
		}
	}//while
	return s;
} //getItem(a)


var taAnsi = document.getElementById('taAnsi');
var taUni = document.getElementById('taUni');

function tran()
{
	var a = taAnsi.value.replace('\t',' ').split('\n');
	for (var i = 0; i < a.length; i++)
	{
		a[i] = a[i].trim();

		if (';'==a[i][a[i].length-1])
		{
			a[i]=a[i].substr(0,a[i].length-1);
		}

		a[i] = getItem(a[i]);

		if (1< a[i].length)
		{
			taUni.value += a[i][1] + ansiStr2UniStr(a[i][1]) + "\n";			
		}

	}//for
}//tran()


</script>
 </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值