Weil Jimmer's BlogWeil Jimmer's Blog


PHP/VB__ASCII轉換器

-
更新於 2017-01-16 18:55:54

關於這點,真的是有夠麻煩的,我經常寫php,php通常UTF-8的網頁都是 "一個中文字"

轉成三個Ascii碼,現在才開始研究,到底要怎樣才可以像 VB 一樣 轉成 5位數的代碼。

又或者 VB 轉成 UTF-8形式的 3 個 ASCII。

好吧,結果出來了。

Unicode 介於 2048(800) ~ 65535(ffff) 之間:

套用形式:

1110****

10******

10******

比如:「我」,Unicode 為 25105 (6211),介於上述的情況。

所以:轉成 2進位 110001000010001

切成 三等分,第一等分 四位數,第二等分 六位數,第三等份六位數。不足則補0。

所以變成 0110001000010001,切割後變成:

0110

001000

010001

套進去變成

11100110

10001000

10010001

轉回 10 進位:

230

136

145

形成 UTF8形式的 三個ASCII碼。

另外,介於 128(80) ~ 2047(7ff) 間 則是另一個形式。

110*****

10******

大概就這樣。

有夠複雜的。

其實網頁 也可以 直譯 ASCII碼。

ABC

ABC

這是測試

這是測試

我自己本身也寫了 PHP函數 轉 成 像 VB那樣的UTF16

function string_to_utf16_ascii_HTML($str){
	for ($i=0;$str[$i]!="";$i++){
		$b[]=ord($str[$i]);
	}
	$str2=';&#';
	for ($i=0;$str[$i]!="";$i++){
		$xa=str_pad(decbin($b[$i]),8,'0',STR_PAD_LEFT);
		if (substr($xa,0,3) == "110"){
			$xb=str_pad(decbin($b[$i+1]),8,'0',STR_PAD_LEFT);
			$xx1=substr($xa,3);
			$xx2=substr($xb,2);
			$c=$c.$str2.bindec($xx1.$xx2);
		}elseif (substr($xa,0,4) == "1110"){
			$xb=str_pad(decbin($b[$i+1]),8,'0',STR_PAD_LEFT);
			$xc=str_pad(decbin($b[$i+2]),8,'0',STR_PAD_LEFT);
			$xx1=substr($xa,3);
			$xx2=substr($xb,2);
			$xx3=substr($xc,2);
			$c=$c.$str2.bindec($xx1.$xx2.$xx3);
		}elseif (substr($xa,0,2) == "10"){
		}else{
			$c=$c.$str2.$b[$i];
		}
	}
	return substr($c,1);
}

再次更新,這個PHP代碼速度更快。

function string_to_utf16_ascii_HTML($str){
	for ($i=0;$i<strlen($str);$i++){
		$k=ord($str[$i]);
		if ($k >= 192 and $k <= 223){
			$k=$k-192;
			$k2=ord($str[$i+1])-128;
			$c=$c.'&#'.str_pad($k*64+$k2,5,'0',STR_PAD_LEFT).';';
		}elseif ($k >= 224 and $k <= 239){
			$k=$k-224;
			$k2=ord($str[$i+1])-128;
			$k3=ord($str[$i+2])-128;
			$c=$c.'&#'.str_pad($k*4096+$k2*64+$k3,5,'0',STR_PAD_LEFT).';';
		}elseif ($k >= 128 and $k <= 191){
		}else{
			$c=$c.'&#'.str_pad($k,5,'0',STR_PAD_LEFT).';';
		}
	}
	return $c;
}

又寫了等價VB的ChrW於PHP版。

function ChrW($str){
	$str=intval($str);
	$binnum=decbin($str);
	if ($str<=255){ return chr($str); }elseif($str>=256 and $str<=2047){
		$binnum=str_pad($binnum,11,'0',STR_PAD_LEFT);
		$x1=bindec('110'.substr($binnum,0,5));
		$x2=bindec('10'.substr($binnum,5,6));
		return chr($x1).chr($x2);
	}else{
		$binnum=str_pad($binnum,16,'0',STR_PAD_LEFT);
		$x1=bindec('1110'.substr($binnum,0,4));
		$x2=bindec('10'.substr($binnum,4,6));
		$x3=bindec('10'.substr($binnum,10,6));
		return chr($x1).chr($x2).chr($x3);
	}
}

By Weil Jimmer


This entry was posted in Functions, PHP, VB.NET By Weil Jimmer.

About Weil Jimmer

avatar

Hi! Everyone! My name is Weil Jimmer. This is my personal blog. I'm a webmaster of this site. I hope the site will be popular. Now, Let's go! Enjoy gaining more knowledge.
More Details About Me : https://weils.net/profile.php

Leave a Reply

Name*:

Email*:

Website:

Privacy:   

Comment*:

Visitor Count

pop
nonenonenone

Note

台灣假新聞橫行,沒一家霉體能信的,網軍側翼到處洗風向,堪憂。

毋忘初心,
絕不利慾薰心。

支持網路中立性.
Support Net Neutrality.

飽暖思淫欲,
饑寒起盜心。

支持臺灣實施無條件基本收入

歡迎前來本站。

Words Quiz


Search

Music

Blogging Journey

4478days

since our first blog post.

The strong do what they can and the weak suffer what they must.

Privacy is your right and ability to be yourself and express yourself without the fear that someone is looking over your shoulder and that you might be punished for being yourself, whatever that may be.

It is quality rather than quantity that matters.

I WANT Internet Freedom.

Reality made most of people lost their childishness.

Justice,Freedom,Knowledge.

Without music life would be a mistake.

Support/Donate

This site also need a little money to maintain operations, not entirely without any cost in the Internet. Your donations will be the best support and power of the site.
MethodBitcoin Address
bitcoin1gtuwCjjVVrNUHPGvW6nsuWGxSwygUv4x
buymeacoffee
Register in linode via invitation link and stay active for three months.Linode

Support The Zeitgeist Movement

The Zeitgeist Movement

The Lie We Live

The Lie We Live

The Questions We Never Ask

The Questions We Never Ask

Man

Man

THE EMPLOYMENT

Man

In The Fall

In The Fall

Facebook is EATING the Internet

Facebook

Categories

Android (7)

Announcement (4)

Arduino (2)

Bash (2)

C (3)

C# (5)

C++ (1)

Experience (52)

Flash (2)

Free (13)

Functions (36)

Games (13)

General (60)

Git (2)

HTML (7)

Java (13)

JS (7)

Mood (24)

NAS (2)

Note (32)

Office (1)

OpenWrt (6)

PHP (9)

Privacy (4)

Product (12)

Python (4)

Software (11)

The Internet (25)

Tools (16)

VB.NET (8)

WebHosting (7)

Wi-Fi (5)

XML (4)