如何检测字符串是否为Base64Encoded? [英] How to detect if a string is Base64Encoded or not?

查看:259
本文介绍了如何检测字符串是否为Base64Encoded?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个是检测字符串是否是Base64Encoded的最佳方法(使用Delphi)?

Which is the best method to detect if a string is Base64Encoded or not (using Delphi)?

推荐答案

您可以检查字符串是否只包含 Base64 valids chars

You can check if the string only contains Base64 valids chars

function StringIsBase64(const InputString : String ) : Boolean;
const
  Base64Chars: Set of AnsiChar = ['A'..'Z','a'..'z','0'..'9','+','/','='];
var
  i : integer;
begin
  Result:=True;
   for i:=1 to Length(InputString) do
   {$IFDEF UNICODE}
   if not CharInSet(InputString[i],Base64Chars) then
   {$ELSE}
   if not (InputString[i] in Base64Chars) then
   {$ENDIF}
   begin
     Result:=False;
     break;
   end;
end;

= char用于填充您可以在填充的base64字符串的函数中添加一个aditional valiation,以检查字符串的长度是否为mod 4

The = char is used for padding so you can add an aditional valiation to the function for padded base64 strings checking if the length of the string is mod 4

这篇关于如何检测字符串是否为Base64Encoded?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆