将十六进制值转换为字符串 [英] Converting Hexadecimal values into string

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

问题描述

大家好,

我有一个模板富文本框,试图在其中放置带格式的文本(如粗体,斜体并更改文本的颜色).我将格式化后的文本从此框保存到类型为''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.

推荐答案

如果您在VB.Net中使用Solution1,请使用以下代码:
If you are using Solution1 in VB.Net Use Following Code :
'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)


您要做的就是将字符串转换为字节[],并将其保存为图像数据字段.
All you have to do is reverse the conversion from string to byte[] which you did in order to save it as an Image data field.
//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);


没有读太多东西(看起来不太合理):问题的标题根本没有道理.什么转换成什么?任何数字类型都不能是十六进制"或十进制"或八进制"或类似的东西,它们都是二进制".仅当这是一些字符串,其内容应该表示数字的十六进制表示形式时,才可以谈论十六进制".那么,应该转换什么:将此字符串转换为字符串?

请不要说您在问题的正文中进行了解释:首先,您没有,其次,标题应该足够合理,否则没有足够的理由来阅读正文.

—SA
Without reading much of this post (which does not look reasonable): the title of the question does not make sense at all. What convert to what? No numeric type can be "hexadecimal" or "decimal" or "octal" or something like that — they all are "binary". You can only talk about "hexadecimal" only if this is some string which content is supposed to represent a hexadecimal presentation of a number. So, what should be converted: this string to string?

Please don''t say that you explain it in the body of the question: first, you don''t, second, a title should be reasonable enough, otherwise there is no enough reason to read the body.

—SA


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

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