2018年9月26日 星期三

Delphi 執行 Escape編碼及解碼


function Escape(Str: string): string;
var
  I: Integer;
  w: Word ;
begin
  Result := '';
  for I := 1 to Length(Str) do
  begin
    w := Word(Str[I]) ;

    if w in [Ord('0')..Ord('9'),Ord('A')..Ord('Z'),Ord('a')..Ord('z')] then
      Result := Result + Char(w)
    else if w <= 255 then
      Result := Result + '%' + IntToHex(w,2)
    else
      Result := Result + '%u' + IntToHex(w,4);
  end;
end;


function Unescape(const Str:String):String;
  // Sub Function Begin =======================
  function UnescapeUncodeChar(const s:String):WideChar;
  var
    r:Array [0..1] of Byte;
  begin
    HexToBin(
             PChar(LowerCase(s)),
             @r,
             SizeOf(r)
             );
    Result:=WideChar((r[0] shl 8) or r[1]);
  end;
  function UnescapeAnsiChar(const s:String):Char;
  begin
    HexToBin(
             PChar(LowerCase(s)),
             @Result,
             SizeOf(Result)
             );
  end;
  // Sub Functionn End ======================
var
  I:Integer;
  C:Integer;
begin
  C:=1;
  SetLength(Result,Length(Str));

  I:=1;
  while I <= Length(Str) do
  begin
    if Str[I] = '%' then
    begin
      if (I < Length(Str)) and (Str[I+1]='u') then
      begin
        Result[C] := UnescapeUncodeChar(
                                     Copy( Str,I+2,4 )
                                     );//Do with '%uxxxx'
        Inc(i,6);
      end
      else
      begin
        Result[C] := Char(
                           UnescapeAnsiChar( Copy(Str,I+1,2) )
                           );//Do with '%xx'
        Inc(I,3);
      end;
    end
    else
    begin
      Result[C] := WideChar(Str[I]);
      //
      Inc(I);
    end;
    Inc(C);
  end;

  SetLength(Result,C-1);

end;

2018年3月25日 星期日

ubuntu 16.04.x 安裝MySql 5.6

因為ubuntu 16.04預設的MySql版本為5.7,若要改成安裝5.6的版本,可以使用下列方式執行。

sudo add-apt-repository 'deb http://archive.ubuntu.com/ubuntu trusty universe'
sudo apt-get update
sudo apt install mysql-server-5.6 
sudo apt install mysql-client-5.6

若安裝後,開機無法自動執行,可以執行下列指令:
sudo update-rc.d -f mysql defaults

2018年1月13日 星期六

Ubuntu 16.4 x64 安裝 Lazaurs 1.8

1. 先下載 Lazaurs 1.8 安裝套件,下載連結 ,檔案包含如下:

  • lazarus-project_1.8.0-1_amd64.deb
  • fpc-src_3.0.4-2_amd64.deb
  • fpc_3.0.4-2_amd64.deb

2. 因為要使用 gdebi 進行套件安裝,所以Ubuntu需要有包含此套件,若沒有,則需要先進安裝。可以直接使用 Ubuntu Software 進行套件查詢及安裝。

3. 開始安裝 Lazarus前,需要先安裝 fpc套件
    $ sudo gdebi fpc_3.0.4-2_amd64.deb
    $ sudo gdebi fpc-src_3.0.4-2_amd64.deb

    安裝 lazarus-project_1.8.0-1_amd64.deb
    $ sudo gdebi lazarus-project_1.8.0-1_amd64.deb

    安裝後,建議重開機。




2017年11月28日 星期二

Ubuntu 設定網卡IP

1. 修改Ethernet網路IP

$ sudo vi /etc/network/interfaces  -> 進入修改編輯畫面

********************************************
auto lo
iface lo inet loopback

auto eth0   #網卡名稱
iface eth0 inet static   # static : 靜態IP, dhcp : 動態IP
address 192.168.0.10  # IP Address
netmask 255.255.255.0 # 網路遮罩
gateway 192.168.0.1 # 預設閘道

# 設定 DNS Server
dns-nameservers 8.8.8.8 168.95.1.1

********************************************

2. 設定DNS --> 在這邊設定,有時會遇到重啟後,設定就不見了

$ sudo vi /etc/resolv.conf

********************************************

nameserver 168.95.1.1 
nameserver 168.95.192.1

**********************************************

2. 修改後,重新啟動網卡

$ sudo /etc/init.d/networking restart

2017年11月7日 星期二

Oracle 帳號鎖定解除

Oracle預設在密碼連續錯誤10次時,該帳戶便會被鎖定,可以透過下列方式進行處理:

1. 使用sqlplus,透過 sysdba角色登入資料庫
    Ex: sqlplus system / as sysdba

2. 查詢帳戶被鎖定時間方式:
    /*  修改時間顯示格式,方便閱讀。 */
    alter session set nls_date_format = 'yyyy-MM-dd hh24:mi:ss' ; 
   /* 查詢帳號被鎖定的時間 */
   SELECTusername,lock_date FROM dba_users WHERE username like '帳號' ;

3. 解除帳號的鎖定:
    alter user MyAccunt account unlock ;
 
 

2017年6月21日 星期三

Delphi 判斷字串是否為中文字

可以透過 ByteType函式來判斷字串的內容,藉以辨識每個字節的型態。

function ByteType(const S: AnsiString; Index: Integer): TMbcsByteType;
function ByteType(const S: UnicodeString; Index: Integer): TMbcsByteType;

回傳內容:
mbSingleByte : 單字節字串
mbLeadByte   : 雙字節(中文字)字串的第一個字符
mbTrailByte  : 雙字節(中文字)字串的第二個字符

範例:
var
  Str, S1,S2 : String ;    // AnsiString
  I : integer ;
begin
  Str := '12判ew斷dd某一ccc個111' ;
  S1 := '' ;
  S2 := '' ;
  for I := 1 to Length(Str) do
  begin
    if ByteType(Str,I) = mbSingleByte then
      S1 := S1 + Str[I]
    else
    if ByteType(Str,I) in [mbLeadByte,mbTrailByte] then
      S2 := S2 + Str[I] ;

  end;
  // 輸出結果
  S1 -->  12ewddccc111
  S2 -->  判斷某一個
end ;

2017年2月7日 星期二

PowerShell 將主機設定允許遠端管理

PowerShell 將主機設定允許遠端管理

要使用PowerShell進行遠端主機管理時,被管理端必須要允許可以進行遠端管理,可以參考下列網址說明:
https://technet.microsoft.com/en-us/library/ff700227.aspx

節錄該網站部分內容如下:

You can verify the availability of WinRM and configure a PowerShell for remoting by following these steps: 
1. Start Windows PowerShell as an administrator by right-clicking the Windows PowerShell shortcut and selecting Run As Administrator. 

2. The WinRM service is confi gured for manual startup by default. You must change the startup type to Automatic and start the service on each computer you want to work with. At the PowerShell prompt, you can verify that the WinRM service is running using the following command: 
get-service winrm
The value of the Status property in the output should be “Running”.

3. To configure Windows PowerShell for remoting, type the following command: 
Enable-PSRemoting –force

In many cases, you will be able to work with remote computers in other domains. However, if the remote computer is not in a trusted domain, the remote computer might not be able to authenticate your credentials. To enable authentication, you need to add the remote computer to the list of trusted hosts for the local computer in WinRM. To do so, type: 
winrm s winrm/config/client '@{TrustedHosts="RemoteComputer"}'
Here, RemoteComputer should be the name of the remote computer, such as: 
winrm s winrm/config/client '@{TrustedHosts="CorpServer56"}'


另外,若是執行上述程序後,遠端連結到主機端,還是會出現 Access is denied (拒絕存取),很有可能是UAC的因素,依據網路上找到的說明,若是主機有啟用UAC,遠端連線控制所使用的帳戶,若是網域帳戶,且在Administrators群組,則不受影響,但若使用的帳戶是屬於LocalAccount,則UAC不允許遠端訪問WinRM服務,若需要使用,則需要建Registry中建置下列值DWORd設定為1,以允許LocalAccount執行WinRM服務

[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System] LocalAccountTokenFilterPolicy

或者透過系統管理員身分執行CMD,輸入下列指令新增Registry參數:

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f" and then "winrm quickconfig