本文介绍了将十六进制值转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个模板富文本框,试图在其中放置带格式的文本(如粗体,斜体并更改文本的颜色).我将格式化后的文本从此框保存到类型为''image''
的数据库中的列中
之所以将其保存为Hexadecimal值,是因为我觉得将其保存为html会创建很多编码来解码每个html标签并显示数据.

现在,当我尝试检索相同的值并转换为字符串时,它无法连接到字符串.它带回来的只是string.byte[].

如何将该十六进制字符串转换为正确的字符串,以便可以再次在富文本框中显示它?


保存文本的代码

Hello All,

I have a template rich text box where I am trying to put the formatted text (Like bolding, Italicizing and changing the colour of the text). I am saving the formatted text from this box to a column in the database which is of type ''image''

The reason behind me saving this as an Hexadecimal value is because I feel saving it as html will create a lot of coding to decode each and every html tags and show the data.

Now when I am trying to retrieve the same value and convert to string, It is unable to connect to string. all it brings back is string.byte[].

How can I convert this hexstring to proper string so that I can show it in the rich text box again?


Code to save Text

Private Sub SignSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SignSave.Click
        Dim sqladd As New SqlCommand
        Dim signChecked As Integer
        If SignDefaultCheckBox.Checked Then
            signChecked = 1
        Else
            signChecked = 0
        End If
        Sqlcon.ConnectionString = "Data Source=KRISHI-PC\SQLEXPRESS;Initial Catalog=gmail;Persist Security Info=True;User ID=sa;Password=manager"
        Try
            Dim RTBXML As String = Sample_Mail.ConvertToHTML(SignTemplateRichTextBox)
            Sqlcon.Open()
            sqladd.Connection = Sqlcon
            sqladd.CommandText = "insert into dbo.signatures values (' " & SignatureTemplateNameTextBox.Text & " ' , '" & RTBXML & "'," & signChecked & ",0)"
            sqladd.ExecuteNonQuery()
            MsgBox("Data Inserted Successfully.")
            SignDefaultCheckBox.Checked = False
            SignatureTemplateNameTextBox.Text = ""
            SignTemplateRichTextBox.Text = ""
            SignatureListBox.Update()
        Catch ex As Exception
            MsgBox(" Error:" & ex.Message)
        Finally
            Sqlcon.Close()
            DT.Rows.Clear()
            Signature_Template_Load(Nothing, Nothing)
        End Try
    End Sub



加载数据的代码



Code to load data

Private Sub SignatureListBox_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SignatureListBox.SelectedValueChanged
        SignSave.Enabled = True
        SignDelete.Enabled = True
        SignatureTemplateNameTextBox.Clear()
        Dim DTLoad As New DataTable
        Dim bytestring As String
        SignatureTemplateNameTextBox.Text = SignatureListBox.Text
        Sqlcon.ConnectionString = "Data Source=KRISHI-PC\SQLEXPRESS;Initial Catalog=gmail;Persist Security Info=True;User ID=sa;Password=manager"
        Dim SearchQuery As String = "Select sigtemplate from dbo.signatures where sigtemplatename = '" & SignatureTemplateNameTextBox.Text & "'"
        Dim sqlda As New SqlDataAdapter(SearchQuery, Sqlcon)
        sqlda.Fill(DS, "LoadTable")
        DTLoad = DS.Tables("LoadTable")
        If DTLoad.Rows.Count > 0 Then
            bytestring = DTLoad.Rows(0).Item(0).ToString
        End If
        'DS.Tables("LoadTable").Clear()

    End Sub



我现在把所有东西都放好了.



I have put everything now.

推荐答案

'byte() to string:
   Dim bytes As Byte()= ...
   Dim s As String= System.Text.Encoding.ASCII.GetString(bytes)

'string to byte():
   Dim s As String= ...
   Dim bytes As Byte()= System.Text.Encoding.ASCII.GetBytes(s)


将值从十六进制转换为字符串的另一种方法:


Another Way to Convert Value From Hexadecimal to String :

'Format to Convert Type
Convert.ChangeType(Your Value As Object,Type to Convert)
'Example
Convert.ChangeType("Your String", TypeCode.Byte)


//byte[] to string:
   byte[] bytes = ...
   string s = System.Text.Encoding.ASCII.GetString(bytes);

//string to byte[]:
   string s = ...
   byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);



这篇关于将十六进制值转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 17:51
查看更多