android 汉字转字节,安卓汉字转拼音
原来研究的小项目,现在简单整理了一下,免费贡献给大家!
package com.tool.hz2py;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
protected Hz2py hz2py;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hz2py = new Hz2py();
TextView view = (TextView) findViewById(R.id.text);
view.setText(hz2py.hz2py("汉字转拼音"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
jni类:Hz2py
package com.tool.hz2py;
public class Hz2py {
static {
System.loadLibrary("Hz2py");
};
public native String hz2py(String text);
}
下面是C++头文件和代码
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_tool_hz2py_Hz2py */
#ifndef _Included_com_tool_hz2py_Hz2py
#define _Included_com_tool_hz2py_Hz2py
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_tool_hz2py_Hz2py
* Method: hz2py
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_tool_hz2py_Hz2py_hz2py
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
#include "hz2py.h"
#include
#include "com_tool_hz2py_Hz2py.h"
#define HZ2PY_UTF8_CHECK_LENGTH 20
#define HZ2PY_FILE_READ_BUF_ARRAY_SIZE 1
#define HZ2PY_INPUT_BUF_ARRAY_SIZE 1024
#define HZ2PY_OUTPUT_BUF_ARRAY_SIZE 2048
#define HZ2PY_STR_COPY(to, from, count) \
ok = 1;\
i = 0;\
_tmp = from;\
while(i < count)\
{\
if (*_tmp == '\0')\
{\
ok = 0;\
break;\
}\
_tmp ++;\
i ++;\
}\
if (ok)\
{\
i = 0;\
while(i < count)\
{\
*to = *from;\
to ++;\
from ++;\
i ++;\
}\
}\
else\
{\
if (overage_buff != NULL)\
{\
while(*from != '\0')\
{\
*overage_buff = *from;\
from ++;\
}\
}\
break;\
}
//将utf8编码的字符串中的汉字解成拼音
// in 输入
// out 输出
// first_letter_only 是否只输出拼音首字母
// polyphone_support 是否输出多音字
// add_blank 是否在拼音之间追加空格
// convert_double_char 是否转换全角字符为半角字符
// overage_buff 末尾如果有多余的不能组成完整utf8字符的字节,将写到overage_buff,传NULL将输出到out
void utf8_to_pinyin(char *in, char *out, int first_letter_only,
int polyphone_support, int add_blank, int convert_double_char,
char *overage_buff) {
int i = 0;
char *utf = in;
char *_tmp;
char *_tmp2;
char py_tmp[30] = "";
char py_tmp2[30] = "";
char *out_start_flag = out;
int uni;
int ok = 0;
while (*utf != '\0') {
if ((*utf >> 7) == 0) {
HZ2PY_STR_COPY(out, utf, 1);
//如果为一个字节加上#号分隔
*out = '#'; //用#号做为分隔符
out++;
//去掉其它的英文只留汉字
//只能搜索到汉字拼音里面字母
//out--;
//*out = ' ';
}
//两个字节
else if ((*utf & 0xE0) == 0xC0) {
HZ2PY_STR_COPY(out, utf, 2);
}
//三个字节
else if ((*utf & 0xF0) == 0xE0) {
if (*(utf + 1) != '\0' && *(utf + 2) != '\0') {
uni = (((int) (*utf & 0x0F)) << 12)
(((int) (*(utf + 1) & 0x3F)) << 6)
(*(utf + 2) & 0x3F);
if (uni > 19967 && uni < 40870) {
memset(py_tmp, '\0', 30);
memset(py_tmp2, '\0', 30);
strcpy(py_tmp, _pinyin_table_[uni - 19968]);
_tmp = py_tmp;
_tmp2 = py_tmp2;
if (first_letter_only == 1) {
*_tmp2 = *_tmp;
_tmp++;
_tmp2++;
while (*_tmp != '\0') {
if (*_tmp == '' *(_tmp - 1) == '') {
*_tmp2 = *_tmp;
_tmp2++;
}
_tmp++;
}
} else {
strcpy(py_tmp2, py_tmp);
}
_tmp2 = py_tmp2;
if (polyphone_support == 0) {
while (*_tmp2 != '\0') {
if (*_tmp2 == '') {
*_tmp2 = '\0';
break;
}
_tmp2++;
}
_tmp2 = py_tmp2;
}
strcpy(out, _tmp2);
out += strlen(_tmp2);
if (add_blank) {
*out = '#'; //用#号做为分隔符
out++;
}
utf += 3;
} else if (convert_double_char && uni > 65280 && uni < 65375) {
*out = uni - 65248;
out++;
utf += 3;
} else if (convert_double_char && uni == 12288) {
*out = 32;
out++;
utf += 3;
} else {
HZ2PY_STR_COPY(out, utf, 3);
}
} else {
HZ2PY_STR_COPY(out, utf, 3);
}
}
//四个字节
else if ((*utf & 0xF8) == 0xF0) {
HZ2PY_STR_COPY(out, utf, 4);
}
//五个字节
else if ((*utf & 0xFC) == 0xF8) {
HZ2PY_STR_COPY(out, utf, 5);
}
//六个字节
else if ((*utf & 0xFE) == 0xFC) {
HZ2PY_STR_COPY(out, utf, 6);
} else {
if (overage_buff != NULL) {
*overage_buff = *utf;
overage_buff++;
} else {
HZ2PY_STR_COPY(out, utf, 1);
}
break;
}
}
}
//判断一个字符串是否为utf8编码
int is_utf8_string(char *utf) {
int length = strlen(utf);
int check_sub = 0;
int i = 0;
if (length > HZ2PY_UTF8_CHECK_LENGTH) {
length = HZ2PY_UTF8_CHECK_LENGTH;
}
for (; i < length; i++) {
if (check_sub == 0) {
if ((utf[i] >> 7) == 0) {
continue;
} else if ((utf[i] & 0xE0) == 0xC0) {
check_sub = 1;
} else if ((utf[i] & 0xF0) == 0xE0) {
check_sub = 2;
} else if ((utf[i] & 0xF8) == 0xF0) {
check_sub = 3;
} else if ((utf[i] & 0xFC) == 0xF8) {
check_sub = 4;
} else if ((utf[i] & 0xFE) == 0xFC) {
check_sub = 5;
} else {
return 0;
}
} else {
if ((utf[i] & 0xC0) != 0x80) {
return 0;
}
check_sub--;
}
}
return 1;
}
int hztpy(const char *read_buff, char *outbuf) {
char overage_buff[7] = { 0 };
char *_tmp = NULL;
char inbuf[HZ2PY_INPUT_BUF_ARRAY_SIZE] = { 0 };
int add_blank = 1;
int polyphone_support = 1;
int first_letter_only = 0;
int convert_double_char = 0;
// first_letter_only 是否只输出拼音首字母
// polyphone_support 是否输出多音字
// add_blank 是否在拼音之间追加空格
// convert_double_char 是否转换全角字符为半角字符
// overage_buff 末尾如果有多余的不能组成完整utf8字符的字节,将写到overage_buff,传NULL将输出到out
_tmp = inbuf;
if (strlen(overage_buff)) {
strcpy(_tmp, overage_buff);
_tmp += strlen(overage_buff);
memset(overage_buff, '\0', 7);
}
strcpy(_tmp, read_buff);
if (!is_utf8_string(inbuf)) {
return -1;
}
utf8_to_pinyin(inbuf, outbuf, first_letter_only, polyphone_support,
add_blank, convert_double_char, overage_buff);
return 1;
}
JNIEXPORT jstring JNICALL Java_com_tool_hz2py_Hz2py_hz2py(JNIEnv *env,
jobject thiz, jstring text) {
const char* pText = env->GetStringUTFChars(text, 0);
char* oText = new char[512];//256中文
memset(oText,0,512);
hztpy(pText,oText);
jstring returnText = env->NewStringUTF(oText);
env->ReleaseStringUTFChars(text,pText);
delete oText;
return returnText;
}
头文件有点大,我直接上传给大家下载好了。hz2py直接编译好的.so共享库和java文件,注意,包名只能用这个,不能更改。libHz2py
android 汉字转字节,安卓汉字转拼音相关推荐
- 微型计算机一个汉字多少字节,一个汉字多少字节(Byte)?
2006-01-07 一个汉字有几个字节? 依据编码形式: GB-231280 编码为 2个字节(Byte) 包含了 20902 个汉字,其编码范围是 0x8140-0xfefe. GB18030-2 ...
- android 儿童 汉字 学习 游戏,儿童学汉字游戏app官方下载-儿童学汉字游戏v3.7 安卓版-腾牛安卓网...
儿童学汉字游戏app是一款针对学龄前儿童设计的认汉字的应用,软件设计了用卡片和语音相结合的方式进行展示,结合一些有趣的小游戏,轻松快乐地认识汉字.帮助孩子轻松上小学. . 应用介绍 儿童学汉字游戏是一 ...
- java 汉字字典,获取汉字拼音,拼音首字母,五笔,笔画,笔画顺序
最新的可以下载的包(原来写的丢失了,重新写了个): http://download.csdn.net/detail/wssiqi/6394057 如果地址不可访问,查看我的资源,看还在不.~~~ 环境 ...
- bit、byte、位、字节、汉字、字符之间的区别
package com.suypower.chengyu.test; public class ByteTest { /** * byte 8 bits -128 - + 127 * 1 bi ...
- 如何把汉字转成五笔与拼音(首字母或全部字母)
备注:其中的PY,WB为码表:dll找不到上传得地方 using System; using System.IO; using System.Collections; namespace ChsHel ...
- android 儿童 汉字 学习 游戏,儿童学汉字游戏app
儿童学汉字游戏app是专为儿童学习开发的一款应用让孩子们在游戏里面认知汉字文化,学习汉字文化和传播汉字文化,让他们接受中国汉文化知识的洗礼,成为一个爱祖国的好孩子. 应用介绍 儿童学汉字游戏课程体系汉 ...
- Java bit、byte、位、字节、汉字、字符
============================================================================== package com.suypower. ...
- 英文字母和汉字的字节长度问题
英文字母和中文汉字在不同字符集编码下的字节数 英文字母: 字节数 : 1;编码:GB2312 字节数 : 1;编码:GBK 字节数 : 1;编码:GB18030 字节数 : 1;编码:ISO-8859 ...
- C--中文汉字占用字节长度(字符集和字符编码)
中文汉字占用字节长度 一.字符集和字符编码 1.概念 2.英文字母和中文汉字在不同字符集编码下的字节数 二.环境对应的字符编码 1.Ubuntu16.04虚拟机 2.Notepad++ 三.sizeo ...
最新文章
- Promise和Async-Await的入门教程
- Minetorch教程
- mysql5.6允许远程服务器访问数据库
- 一般试卷的纸张大小是多少_一般试卷的纸张大小
- thinkPhp 3.1.3的验证码无法显示的问题
- Theano 中文文档 0.9 - 5.1 Ubuntu安装说明
- 交互进CMU后可以学计算机吗,转专业必看!申请计算机的先修课要求,以CMU为例...
- hadoop配置文件_Hadoop分布式集群
- PHP 短信验证码:发送及验证
- 量化交易软件 python_我用Python做了个量化交易工具!
- 用Python下载抖音无水印视频
- 使用组件,一直报错Unknown custom element: <etregister> - did you register the component correctly?
- XSS朝花夕拾代码简单分析(XSSgame)
- excel行列突出显示_在Excel中突出显示即将到来的日期
- 深圳Python学习:Python几大问,你想知道的答案都在这里!-千锋
- 芯片Tapeout之数字后端实现Review
- Android Softap Mac地址随机化
- matplotlib折线图(设置图片大小和图片保存)
- 理解稀疏矩阵存储形式
- 平板电脑服务器的安装系统安装,平板电脑安装win8系统的具体方法
热门文章
- 屏蔽csdn百度推广广告
- 玩转华为ENSP模拟器系列 | 配置RSTP功能示例
- 即时通讯(IM)开源项目OpenIM每周迭代版本发布-音视频实时通话-v2.0.4
- html群聊插件,团队群聊.html
- Ubuntu20.04+GTX1060+显卡驱动+CUDA11.8+cuDNN8.5.0
- 如何批量一键下单寄快递
- 实例讲解FusionInsight MRS RTD 实时决策引擎在医保行业应用
- SYSML语言OMG认证考试经验分享
- 白衣观音大士灵感神咒
- Android系统框架-Androi的面试必问部分