首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >AS3 unicode

AS3 unicode

作者头像
py3study
发布2020-01-10 17:43:52
发布2020-01-10 17:43:52
9860
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
package com.game.common
{
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	/**
	 * Unicode字符工具
	 * @author Kayer
	 * */
	public final class GameUnicodeTools
	{
		private static var ins : GameUnicodeTools;
		public static function get instance() : GameUnicodeTools
		{
			if (!ins)
			{
				ins = new GameUnicodeTools();
			}
			return ins;
		}
		
		public function GameUnicodeTools():void
		{
			if( ins )
				throw Error("GameUnicodeTools 类被设计成单例!");
		}
		/**解析*/
		public function decode( resBytes : ByteArray , len : uint ) : String
		{
			var bytes : ByteArray  = new ByteArray();
			bytes.endian = Endian.LITTLE_ENDIAN;
//			var surplusLen : uint = ( resBytes.length - resBytes.position + 1 > len ) ? len : resBytes.length - resBytes.position + 1;
			var surplusLen : uint = ( resBytes.length - resBytes.position > len ) ? len : resBytes.length - resBytes.position;
			bytes.writeBytes( resBytes , resBytes.position , surplusLen );
			resBytes.position = resBytes.position + surplusLen - 1;
//			bytes.writeByte(0);
//			bytes.position = 0;
			return this.byArrayToString( bytes );
		}
		/**
		 * 加密</br>
		 * str to unicodeBytes
		 * */
		public function encode( resStr : String , len : uint = 0  ):ByteArray
		{
			return this.StrToByteArray( resStr , ( len > 0 ) ? len : 0  );
		}
		
		//unicode string to ByteArray 
		private function StrToByteArray( strValue : String, uLen:uint = 0 ):ByteArray
		{
			var byAaryR:ByteArray = new ByteArray();
			byAaryR.endian = Endian.LITTLE_ENDIAN;
			for ( var i:int = 0; i < strValue.length; ++i )
			{
				byAaryR.writeShort(strValue.charCodeAt(i));
			}
			for ( i = 0; i < (uLen - strValue.length); ++i )
			{
				byAaryR.writeShort(0);
			}
			return byAaryR;
		}
		//ByteArray to unicode string
		private function byArrayToString( byArray:ByteArray ):String
		{
			byArray.position = 0;
			var strReturn:String = new String();
			var strRep:String = "";
//			var $cacthBytes : ByteArray = new ByteArray();
//			$cacthBytes.endian = Endian.LITTLE_ENDIAN;
			var ss : uint = 0;
			for(var i:int = 0; i <  Math.floor( byArray.length / 2 ) ; i += 1 )
			{
//				$cacthBytes.clear();
//				$cacthBytes.position = 0;
				ss = byArray.readShort();
				if( ss == 0 )
				{
					break;
				}
				else
				{
					byArray.position -= 2;
					if( ss <= 0xff )
					{
						strRep = byArray.readMultiByte( 1 , "utf-8" );
						byArray.position += 1;
					}
					else
					{
						strRep = byArray.readMultiByte( 2 , "unicode" );
					}
	//				strReturn = replaceAt(strReturn, strRep, i, i);
					strReturn += strRep;
				}
			}
//			var strRep:String = byArray.readMultiByte(byArray.length, "unicode");
			return strReturn;
		}
		private function replaceAt(char:String, value:String, beginIndex:int, endIndex:int):String 
		{
			beginIndex = Math.max(beginIndex, 0);
			endIndex = Math.min(endIndex, char.length);
			var firstPart:String = char.substr(0, beginIndex);
			var secondPart:String = char.substr(endIndex, char.length);
			return (firstPart + value + secondPart);
		}
	}
}
代码语言:javascript
复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档