汉字助记码,你会了吗?

汉字助记码,你会了吗?


在编程中,我们经常会遇到汉字助记码的问题,笔者曾经为此多次发愁,现总结前辈的好东西,记录于此,希望能帮助到您,方法有多种,在此比较几种方案,简单剖析一下。

首先说明,什么是汉字助记码?所谓的汉字助记码就是一个汉字的拼音的首字母,如:张的汉字助记码为Z,湖北中医药大学的助记码为HBZYYDX。下面通过程序用三种方法实现:

方法一:表获取方法;

表内容大致说明:      

实现核心代码——SQL标量值函数:


 1 ------------------------------------------------
2 --作者:zhangbc
3 --时间:2014-05-05
4 --功能:获取汉字拼音首字母
5 ------------------------------------------------
6 ALTER function [dbo].[fun_getMnemonic](@str nvarchar(4000))
7 returns nvarchar(4000)
8 as
9 begin
10 declare @zjm varchar(100),@tmp_char varchar(2),@tmp_zjm varchar(2), @i int,@length int
11 set @zjm =‘‘
12 set @length = len(@str)
13 set @i = 1
14 while @i<=@length
15 begin
16 set @tmp_char = substring(@str,@i,1)
17 select @tmp_zjm =zjm from hz_zjm where hanzi=@tmp_char
18 if @@rowcount=1
19 set @zjm = @zjm +Rtrim(@tmp_zjm)
20 set @i = @i + 1
21 end
22 return (@zjm)
23 end

View
Code

方法二:存储过程获取;

实现核心代码——SQL存储过程:


 1 -- =============================================
2 -- Author: zhangbc
3 -- Create date: 2014-05-03
4 -- Description: 获取汉字拼音首字母
5 -- =============================================
6 ALTER PROCEDURE [dbo].[getMnemonic]
7 @str varchar(4000)
8 AS
9 -- return char(4000)
10 BEGIN
11 declare @word nchar(1),@PY nvarchar(4000)
12 set @PY=‘‘
13 while len(@str)>0
14 begin
15 set @word=left(@str,1)
16 --如果非汉字字符,返回原字符
17 set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
18 then (select top 1 PY from (
19 select ‘A‘ as PY,N‘驁‘ as word
20 union all select ‘B‘,N‘簿‘
21 union all select ‘C‘,N‘錯‘
22 union all select ‘D‘,N‘鵽‘
23 union all select ‘E‘,N‘樲‘
24 union all select ‘F‘,N‘鰒‘
25 union all select ‘G‘,N‘腂‘
26 union all select ‘H‘,N‘夻‘
27 union all select ‘J‘,N‘攈‘
28 union all select ‘K‘,N‘穒‘
29 union all select ‘L‘,N‘鱳‘
30 union all select ‘M‘,N‘旀‘
31 union all select ‘N‘,N‘桛‘
32 union all select ‘O‘,N‘漚‘
33 union all select ‘P‘,N‘曝‘
34 union all select ‘Q‘,N‘囕‘
35 union all select ‘R‘,N‘鶸‘
36 union all select ‘S‘,N‘蜶‘
37 union all select ‘T‘,N‘籜‘
38 union all select ‘W‘,N‘鶩‘
39 union all select ‘X‘,N‘鑂‘
40 union all select ‘Y‘,N‘韻‘
41 union all select ‘Z‘,N‘咗‘
42 ) T
43 where word>=@word collate Chinese_PRC_CS_AS_KS_WS
44 order by PY ASC) else @word end)
45 set @str=right(@str,len(@str)-1)
46 end
47 select @PY
48 END

View
Code

方法三:标量值获取(算法和方法二一样,实现方式不同):

代码如下:


 1 ------------------------------------------------
2 --作者:zhangbc
3 --时间:2014-03-19
4 --功能:获取汉字拼音首字母
5 ------------------------------------------------
6 ALTER function [dbo].[fun_getZjm](@str nvarchar(4000))
7 returns nvarchar(4000)
8 as
9 begin
10 declare @word nchar(1),@PY nvarchar(4000)
11 set @PY=‘‘
12 while len(@str)>0
13 begin
14 set @word=left(@str,1)
15 --如果非汉字字符,返回原字符
16 set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
17 then (select top 1 PY from (
18 select ‘A‘ as PY,N‘驁‘ as word
19 union all select ‘B‘,N‘簿‘
20 union all select ‘C‘,N‘錯‘
21 union all select ‘D‘,N‘鵽‘
22 union all select ‘E‘,N‘樲‘
23 union all select ‘F‘,N‘鰒‘
24 union all select ‘G‘,N‘腂‘
25 union all select ‘H‘,N‘夻‘
26 union all select ‘J‘,N‘攈‘
27 union all select ‘K‘,N‘穒‘
28 union all select ‘L‘,N‘鱳‘
29 union all select ‘M‘,N‘旀‘
30 union all select ‘N‘,N‘桛‘
31 union all select ‘O‘,N‘漚‘
32 union all select ‘P‘,N‘曝‘
33 union all select ‘Q‘,N‘囕‘
34 union all select ‘R‘,N‘鶸‘
35 union all select ‘S‘,N‘蜶‘
36 union all select ‘T‘,N‘籜‘
37 union all select ‘W‘,N‘鶩‘
38 union all select ‘X‘,N‘鑂‘
39 union all select ‘Y‘,N‘韻‘
40 union all select ‘Z‘,N‘咗‘
41 ) T
42 where word>=@word collate Chinese_PRC_CS_AS_KS_WS
43 order by PY ASC) else @word end)
44 set @str=right(@str,len(@str)-1)
45 end
46 return @PY
47 end

View
Code

方法四:字符编码法;

核心代码如下:


 1  public class getMnemonic
2 {
3 public getMnemonic()
4 {
5
6 }
7 /// <summary>
8 /// 字符编码的获取
9 /// </summary>
10 /// <param name="cnChar">字符</param>
11 /// <returns>字符编码</returns>
12 private static string getSpell(string cnChar)
13 {
14 byte[] arrCN = Encoding.Default.GetBytes(cnChar);
15 if (arrCN.Length > 1)
16 {
17 int area = (short)arrCN[0];
18 int pos = (short)arrCN[1];
19 int code = (area << 8) + pos;
20 int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614,
21 48119, 48119, 49062, 49324, 49896,50371, 50614, 50622,
22 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
23 for (int i = 0; i < 26; i++)
24 {
25 int max = 55290;
26 if (i != 25)
27 max = areacode[i + 1];
28 if (areacode[i] <= code && code < max)
29 return Encoding.Default.GetString(new byte[] { (byte)(65 + i)});
30 }
31 return "*";
32 }
33 else return cnChar;
34 }
35 /// <summary>
36 /// 字符编码获取助记码
37 /// </summary>
38 /// <param name="str">汉字</param>
39 /// <returns>助记码</returns>
40 public string getChsSpell(string str)
41 {
42 string myStr = "";
43 for (int i = 0; i < str.Length; i++)
44 {
45 myStr += getSpell(str.Substring(i, 1));
46 }
47 return myStr;
48 }
49 }

View
Code

测试结果如下:

图1-1

图1-2

图1-3

图1-4

小结:

通过测试结果来看,字符编码(方法四)还是有点不完美,其实通过表获取的方法(方法一)也存在不足,汉字存储太少。

这次测试的一个意外收获就是char()与varchar()的区别,请看:

还望知情者给予合理的解释,不甚感激!如果能给你带来帮助,请赞一个,你的用心阅读是我写博的不竭动力!

赠送源码一份,供您研究:http://files.cnblogs.com/zhangbc/MnemonicOfWords.rar

PS:如有不足之处,欢迎指点与切磋,您的光临是我的荣幸,联系方式QQ:649414754 

时间: 2024-08-29 01:41:17

汉字助记码,你会了吗?的相关文章

iOS-如何返回某个字符串的拼音助记码

我也是看了网上的一个示例代码后,在它的基础上进行的修改.因为项目上会用到,我相信很多人的项目上也会用到.所以实现后,也赶紧分享出来,希望后来人不需要花费时间了. 提示:这里用到了正则表达式,使用了一个第三方开源的组件:RegexKitLite (该组件如何使用网上教程很多) #pragma mark - 获取拼音首字母(传入汉字字符串, 返回小写拼音首字母) +(NSString *)pinYingFirstCharactorStr:(NSString *)aString { //转成了可变字符

助记码的全部更新

需要的两个函数: USE [oaerp] GO /****** Object:  UserDefinedFunction [dbo].[f_ch2py]    Script Date: 12/31/2014 17:12:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER function [dbo].[f_ch2py](@chn nchar(1)) returns char(1) as begin declare @

升级7.2后科目表重复,助记码后缀多了重复值_1

问题解决办法1.先备份科目select * into t_bd_account_1125 from T_BD_ACCOUNT 2.查出问题凭证select distinct b.FNUMBER as 帐簿编码,v.FYEAR as 年度,v.FPERIOD as 期间,g.FNAME as 凭证字, v.FVOUCHERGROUPNO as 凭证号 from T_GL_VOUCHERENTRY ve inner join T_GL_VOUCHER v on v.FVOUCHERID=ve.FVO

1.16. BIP39协议:使用助记词生成确定性钱包

以太坊系统学习教程: https://www.netkiller.cn/blockchain/bip39.html BIP:39 层:应用层 标题:使用助记词生成确定性钱包秘钥 作者:Marek Palatinus [email protected] Pavol Rusnak [email protected] Aaron Voisine [email protected] Sean Bowe [email protected] 状态:已经被提议 类型:标准化跟踪 创建日期:2013-09-10

JVM指令集(指令码、助记符、功能描述)(转)

JVM指令集(指令码.助记符.功能描述) 指令码 助记符 功能描述 0x00 nop 无操作 0x01 aconst_null 指令格式:  aconst_null 功能描述:  null进栈. 指令执行前 指令执行后 栈底 ... ... null 栈顶 注意:JVM并没有为null指派一个具体的值. 0x02 iconst_m1 int型常量值-1进栈 0x03 iconst_0 int型常量值0进栈 0x04 iconst_1 int型常量值1进栈 0x05 iconst_2 int型常量

Bytomd 助记词恢复密钥体验指南

比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 背景知识 Bytom 使用的 密钥类型为基于 ed25519 的 chainkd.XPub 代码见 bytom/crypto/ed25519/chainkd 文档见 https://chain.com/docs/1.2/protocol/specifications/chainkd` 预备 代码修改 首先适

助记词是什么,有什么用?

玩加密货币的朋友相信对助记词都不陌生,我们在使用钱包之前,会让你备份12个单词,在备份期间不允许截图操作,并且不断强调这12个单词非常重要,最好用物理方式备份,备份时身边不要有任何人. 对于普通用户来说,如果只是一味的向他们强调助记词重要性的结论,而不告诉背后的原因的话,是很难调动起人的底层动力的,很可能过几天就忘了助记词的重要性(小编已经看过不少在群里呼唤自己因为助记词丢失而导致破产的杯具). 助记词的英文是Mnemonic,在大部分人的印象中,助记词=私钥,是导入钱包的工具,其实准确的说,助

如何开发一款以太坊(安卓)钱包系列1 - 通过助记词创建账号

上周我开源了一款钱包,反映很好,一周时间不到已经快到100 Star.接下来我会几篇系列文章把开发以太坊钱包的核心要点写出来,也算是对代码的一个解读. 写在前面 钱包是使用Android安卓平台编写,使用的是原生代码Java 语言编写, 是基于Java 1.8 版本,也使用了Java 1.8 中一些较新的语言特性,如 Lambda表达式等:另外还较多使用了ReactiveX/RxAndroid响应式编程用法. 在本系列文章中,重点是介绍以太坊钱包账号.交易等逻辑,有时可能会假定读者已经了解And

vc 文字转换到机内码,输入汉字和数字, 输出一串16进制码(数字-〉ASII码,汉字—〉国标码)

// 可以用,此程序实现的是是文字转换到机内码.机内码=国标码+8080H,不过学习了. //此程序是利用汉字在机器内输出就是机内码的原理,直接保存的,其实挺简单. //输入一串汉字和数字的混合字符, 经过程序转换, 对应输出一串16进制码(数字-〉ASII码,汉字—〉国标码) CString temp; GetDlgItemText(IDC_EDIT1,m_hanzi);//将汉字保存到变量m_hanzi unsigned char *b=new unsigned char[m_hanzi.G