簡體   English   中英

為什么Base64字符串在消息框中顯示為空?

[英]Why is a Base64 string displayed as empty in Message Box?

在提交表單之前,我必須將一些HTML源代碼編碼為base64格式,然后在后面的代碼中將其解碼回原始代碼。 這是MsgBox的測試代碼:

MsgBox(HttpContext.Current.Request.Form("encodedSourceCode"))
MsgBox(Convert.ToString(HttpContext.Current.Request.Form("encodedSourceCode").GetType()))
Dim b = Convert.FromBase64String(HttpContext.Current.Request.Form("encodedSourceCode"))
Dim html = System.Text.Encoding.UTF8.GetString(b)
MsgBox(html)

而且我在客戶端腳本中為encodedSourceCode添加了alert()

結果證明是:

第一個MsgBox:空

第二個MsgBox:“ System.String”

最后一個MsgBox:原始HTML源代碼

JS警報對話框顯示base64字符串,該字符串由一堆數字和字母組成。

簡而言之,一切都很好,除了第一個MsgBox,它應該是base64編碼的字符串,但結果是空的。 為什么? 正常嗎

實際上,這並不重要,因為即使最終結果(在解碼之后)也似乎沒有問題,但是我很好奇為什么中間結果沒有顯示為應有的樣子。

我想,字符串似乎太長而沒有“可折疊”字符。 MsgBox切出“最后一個單詞”,什么也不顯示。
這可以確認這一點:

dim test = HttpContext.Current.Request.Form("encodedSourceCode")
MsgBox(test) ' empty
test = test.Substring(0, 20)
MsgBox(test) ' shows the first 20 characters

在LinqPad中進行測試,我得到了大約43.000個字符的限制:

MsgBox("".PadLeft(43000, "a"))
MsgBox("".PadLeft(44000, "a"))
MsgBox("".PadLeft(43000, "a") & " " & "".PadLeft(1000, "a"))

第一:顯示文字。
第2個:顯示一個空框,長度= 44.000
3rd:顯示文本,盡管總長度為44.001,但是可以在空格處包裹。

它與base64字符串絕對無關,因為它們是簡單的字符串。 這里的證明:

    Dim myString = "Hello world, this is just an ɇxâmpŀƏ ʬith some non-ansi characters..."
    Dim myEncoding As Encoding = Encoding.UTF8
    MsgBox(myString)
    Dim myBase64 = Convert.ToBase64String(myEncoding.GetBytes(myString))
    MsgBox(myBase64)
    Dim myStringAgain = myEncoding.GetString(Convert.FromBase64String(myBase64))
    MsgBox(myStringAgain)
    MsgBox(If(StringComparer.Ordinal.Equals(myString, myStringAgain), "same", "different"))

MsgBox(Convert.ToString(HttpContext.Current.Request.Form("encodedSourceCode").GetType()))

由於將類型的名稱轉換為字符串(請參見xxx.GetType() ),因此會導致“ System.String”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM