Weil Jimmer's BlogWeil Jimmer's Blog


PHP/VB__ASCII轉換器
No Comments

Published:
Updated: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

WPA2 已被 KRACK 攻擊破解!請更新 WiFi 軟體。

歡迎前來本站。

Search

Republic Of China
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.

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
bitcoin1NRMVGGpm2T1pmeejisLSEhCXfCefEW9V4
paypal

The Lie We Live

youtube

The Questions We Never Ask

youtube

Support The Zeitgeist Movement

The Zeitgeist Movement

Man

youtube

In The Fall

youtube

Facebook is EATING the Internet

Facebook

Recent Comments

sickcatail on STEAM免費序號

包包 on STEAM免費序號

問問題 on 最棒的手機通訊軟體 Telegram

unocme on STEAM免費序號

窩仔 on STEAM免費序號

Categories

Announcement (4)

Bash (2)

C (1)

C# (4)

C++ (1)

Experience (38)

Flash (2)

Free (10)

Functions (35)

Games (13)

General (44)

HTML (7)

Java (12)

JS (7)

Mood (23)

Note (24)

Office (1)

PHP (9)

Privacy (3)

Product (9)

Python (4)

Software (8)

The Internet (15)

Tools (12)

VB.NET (8)

WebHosting (7)

Wi-Fi (5)

XML (4)