vb.net 在VB.net中将字节数组转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14658885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:09:31  来源:igfitidea点击:

Convert byte array to string in VB.net

vb.netstring

提问by vikas

I have byte data of .doc, .txt, .docx and I want to convert it to string, I did following things but not getting exact result:

我有 .doc、.txt、.docx 的字节数据,我想将其转换为字符串,我做了以下事情但没有得到确切的结果:

Public ByteData As Byte() = // my data
Dim str As String = String.Empty

str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length)
str = Convert.ToBase64String(objCandidateInfo.ByteData)

Edited

已编辑

So now I am converting the same using Word Application, this code is working this is my code

所以现在我正在使用 Word 应用程序转换相同的代码,这段代码正在运行,这是我的代码

 Private Shared ObjwordApp As Word.Application
    Private Shared nullobj As Object = System.Reflection.Missing.Value
    Private Shared doc As Word.Document
    Shared Sub New()
        ObjwordApp = New Word.Application()
    End Sub

    Public Shared Sub InitializeClass()
        ObjwordApp.Visible = False
    End Sub

    Private Shared Sub OpenWordFile(ByVal StrFilePath As Object)
        Try
            ObjwordApp.Visible = False
        Catch ex As Exception
            ObjwordApp = New Word.Application()
        End Try
        Try
            doc = ObjwordApp.Documents.Open(StrFilePath, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj)
        Catch ex As Exception
            CloseWordFile()
            ObjwordApp.Visible = False
        End Try
    End Sub

    Private Shared Sub CopyWordContent()
        Try
            doc.ActiveWindow.Selection.WholeStory()
            doc.ActiveWindow.Selection.Copy()
        Catch ex As Exception
            Clipboard.Clear()
        End Try
    End Sub

    Private Shared Sub CloseWordFile()
        Try
            doc.Close()
        Catch ex As Exception

        End Try
    End Sub

    Public Shared Function ReadWordFile(ByVal StrFilePath As String, ByVal StrDataFormat As String) As String
        Dim StrFileContent = String.Empty
        If (File.Exists(StrFilePath)) Then
            Try
                OpenWordFile(StrFilePath)
                CopyWordContent()
            Catch ex As Exception

            Finally
                CloseWordFile()
            End Try

            Try
                Dim dataObj As IDataObject = Clipboard.GetDataObject()
                If (dataObj.GetDataPresent(StrDataFormat)) Then
                    StrFileContent = dataObj.GetData(StrDataFormat)
                Else
                    StrFileContent = ""
                End If
                Clipboard.Clear()
            Catch ex As Exception

            End Try
        End If
        Return StrFileContent
    End Function

And when I saving the byte array to DB, I call below function and convert it to rtf, it is not converting, when I attach debugger to it dataObjis Nothing

当我保存字节数组DB,我叫下面的功能,并将其转换为RTF,它不转换,当我附上调试器它dataObjNothing

code 1

代码 1

Dim str As String = String.Empty
                Try
                    'str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length)
                    'str = Convert.ToBase64String(objCandidateInfo.ByteData)
                    'str = System.Text.Encoding.ASCII.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length)
                    str = ClsDocumentManager.ReadContent(objCandidateInfo.ByteData, DataFormats.Rtf)
                Catch ex As Exception

                End Try

I save data db in both byte and text format, so when I call it from db (byte value that I save and convert it to rtf), its working the code is

我以字节和文本格式保存数据 db,所以当我从 db(我保存并将其转换为 rtf 的字节值)调用它时,它的工作代码是

Code 2

代码 2

rtbAttachment.Rtf = ClsDocumentManager.ReadContent(byteAttachment, DataFormats.Rtf)

These are the methods in ClsDocumentManagerclass

这些是ClsDocumentManager类中的方法

Public Shared Function GetRandomNo() As Integer
        Dim RandomNo As New Random()
        Return RandomNo.Next(Convert.ToInt32(DateTime.Now().Minute.ToString() & DateTime.Now().Second.ToString() & DateTime.Now().Hour.ToString()))
    End Function

    Public Shared Function ReadContent(ByVal byteArray As Byte(), ByVal StrReadFormat As String) As String
        Dim StrFileContent As String = String.Empty
        Try
            If (Not IsNothing(byteArray)) Then
                Dim StrFileName As String = GetRandomNo().ToString() & ".doc"
                StrFileName = ClsSingleton.aTempFolderName & StrFileName
                If (CreateWordFile(byteArray, StrFileName)) Then
                    StrFileContent = ClsWordManager.ReadWordFile(StrFileName, StrReadFormat)
                    If (File.Exists(StrFileName)) Then
                        File.Delete(StrFileName)
                    End If
                End If
            End If
        Catch ex As Exception

        End Try
        Return StrFileContent
    End Function

Public Shared Function CreateWordFile(ByVal byteArray As Byte(), ByVal StrFileName As String) As Boolean
        Dim boolResult As Boolean = False
        Try
            If (Not IsNothing(byteArray)) Then
                If (Not File.Exists(StrFileName)) Then
                    Dim objFileStream As New FileStream(StrFileName, FileMode.Create, FileAccess.Write)
                    objFileStream.Write(byteArray, 0, byteArray.Length)
                    objFileStream.Close()
                    boolResult = True
                End If
            End If
        Catch ex As Exception
            boolResult = False
        End Try
        Return boolResult
    End Function

Error Codewhile debugging

调试时的错误代码

Dim dataObj As IDataObject = Clipboard.GetDataObject()
                If (dataObj.GetDataPresent(StrDataFormat)) Then
                    StrFileContent = dataObj.GetData(StrDataFormat)
                Else
                    StrFileContent = ""
                End If

`dataObj` is `Nothing` only when calling from **Code 1** 

Updated

更新

**`ClsDocumentManager`**



Imports System.IO

Public Class ClsDocumentManager
    Public Shared Function GetRandomNo() As Integer
        Dim RandomNo As New Random()
        Return RandomNo.Next(Convert.ToInt32(DateTime.Now().Minute.ToString() & DateTime.Now().Second.ToString() & DateTime.Now().Hour.ToString()))
    End Function

    Public Shared Function ReadContent(ByVal byteArray As Byte(), ByVal StrReadFormat As String) As String
        Dim StrFileContent As String = String.Empty
        Try
            If (Not IsNothing(byteArray)) Then
                Dim StrFileName As String = GetRandomNo().ToString() & ".doc"
                StrFileName = ClsSingleton.aTempFolderName & StrFileName
                If (CreateWordFile(byteArray, StrFileName)) Then
                    StrFileContent = ClsWordManager.ReadWordFile(StrFileName, StrReadFormat)
                    If (File.Exists(StrFileName)) Then
                        File.Delete(StrFileName)
                    End If
                End If
            End If
        Catch ex As Exception

        End Try
        Return StrFileContent
    End Function


    Public Shared Function CreateWordFile(ByVal byteArray As Byte(), ByVal StrFileName As String) As Boolean
        Dim boolResult As Boolean = False
        Try
            If (Not IsNothing(byteArray)) Then
                If (Not File.Exists(StrFileName)) Then
                    Dim objFileStream As New FileStream(StrFileName, FileMode.Create, FileAccess.Write)
                    objFileStream.Write(byteArray, 0, byteArray.Length)
                    objFileStream.Close()
                    boolResult = True
                End If
            End If
        Catch ex As Exception
            boolResult = False
        End Try
        Return boolResult
    End Function
End Class

Here is my ClsWordManagerClass

这是我的ClsWordManager班级

Imports System.IO
Imports System.Text

Public Class ClsWordManager
    Private Shared ObjwordApp As Word.Application
    Private Shared nullobj As Object = System.Reflection.Missing.Value
    Private Shared doc As Word.Document
    Shared Sub New()
        ObjwordApp = New Word.Application()
    End Sub

    Public Shared Sub InitializeClass()
        ObjwordApp.Visible = False
    End Sub

    Private Shared Sub OpenWordFile(ByVal StrFilePath As Object)
        Try
            ObjwordApp.Visible = False
        Catch ex As Exception
            ObjwordApp = New Word.Application()
        End Try
        Try
            doc = ObjwordApp.Documents.Open(StrFilePath, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj)
        Catch ex As Exception
            CloseWordFile()
            ObjwordApp.Visible = False
        End Try
    End Sub

    Private Shared Sub CopyWordContent()
        Try
            doc.ActiveWindow.Selection.WholeStory()
            doc.ActiveWindow.Selection.Copy()
        Catch ex As Exception
            Clipboard.Clear()
        End Try
    End Sub

    Private Shared Sub CloseWordFile()
        Try
            doc.Close()
        Catch ex As Exception

        End Try
    End Sub

    Public Shared Function ReadWordFile(ByVal StrFilePath As String, ByVal StrDataFormat As String) As String
        Dim StrFileContent = String.Empty
        If (File.Exists(StrFilePath)) Then
            Try
                OpenWordFile(StrFilePath)
                CopyWordContent()
            Catch ex As Exception

            Finally
                CloseWordFile()
            End Try

            Try
                Dim dataObj As IDataObject = Clipboard.GetDataObject()
                If (dataObj.GetDataPresent(StrDataFormat)) Then
                    StrFileContent = dataObj.GetData(StrDataFormat)
                Else
                    StrFileContent = ""
                End If
                Clipboard.Clear()
            Catch ex As Exception

            End Try
        End If
        Return StrFileContent
    End Function



End Class

So now the problem is When I convert it in following code : look at ByteAttachmetsin arguement, it convert byte to string

所以现在的问题是当我在下面的代码中转换它时:查看ByteAttachmets争论,它将字节转换为字符串

Public Function UpdateCandidateAttachment(ByVal CandidateID As Integer, ByVal ByteAttachmets As Byte(), ByVal StrExtension As String) As Integer
        Dim Result As Integer = -1
        Try
            Dim objDataLayer As New ClsDataLayer()
            Dim str As String = Nothing
            Try
                'str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length)
                'str = Convert.ToBase64String(objCandidateInfo.ByteData)
                'str = System.Text.Encoding.ASCII.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length)
                str = ClsDocumentManager.ReadContent(ByteAttachmets, DataFormats.Rtf)
            Catch ex As Exception

            End Try
            objDataLayer.AddParameter("@CANDIDATE_ID", CandidateID)
            objDataLayer.AddParameter("@ATTACHMENT_DATA", ByteAttachmets)
            objDataLayer.AddParameter("@CREATED_BY", ClsCommons.IntUserId)
            objDataLayer.AddParameter("@EXTENSION", StrExtension)
            Result = objDataLayer.ExecuteNonQuery("TR_PROC_UpdateCandidateAttachment")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Result
    End Function

And when I call it from following code by property : look at objCandidateInfo.ByteData, it is not working.

当我按属性从以下代码调用它时:看objCandidateInfo.ByteData,它不起作用。

Public Function AddUpdateCandidate(ByVal objCandidateInfo As ClsCandidateInfo) As Integer
        Dim Result As Integer = -1
        Try
            If (ClsCommons.IsValidEmail(objCandidateInfo.StrEmail)) Then
                Dim str As String = Nothing
                Try
                    'str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length)
                    'str = Convert.ToBase64String(objCandidateInfo.ByteData)
                    'str = System.Text.Encoding.ASCII.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length)
                    Dim byteAttachment As Byte() = objCandidateInfo.ByteData
                    str = ClsDocumentManager.ReadContent(byteAttachment, DataFormats.Rtf)
                Catch ex As Exception

                End Try
                Dim objDataLayer As New ClsDataLayer()
                objDataLayer.AddParameter("@REQUIREMENT_ID", objCandidateInfo.RequirementId)
                objDataLayer.AddParameter("@Candidate_Name", objCandidateInfo.StrCandidateName)
                objDataLayer.AddParameter("@Current_Organization", objCandidateInfo.StrCurrentCompany)
                objDataLayer.AddParameter("@Current_Designation", objCandidateInfo.StrCurrentDesignation)
                If (objCandidateInfo.StrExp.Trim() = "") Then
                    objDataLayer.AddParameter("@Overall_Exp", DBNull.Value)
                Else
                    Dim DecExp As Decimal = -1
                    If (Decimal.TryParse(objCandidateInfo.StrExp, DecExp)) Then
                        objDataLayer.AddParameter("@Overall_Exp", DecExp)
                    Else
                        objDataLayer.AddParameter("@Overall_Exp", DBNull.Value)
                    End If
                End If
                objDataLayer.AddParameter("@Qualification", objCandidateInfo.StrQualification)
                objDataLayer.AddParameter("@Location", objCandidateInfo.StrCurrentLocation)
                objDataLayer.AddParameter("@Current_CTC", objCandidateInfo.StrCurrentCTC)
                objDataLayer.AddParameter("@Expected_CTC", objCandidateInfo.StrExpectedCTC)
                objDataLayer.AddParameter("@Phone_No", objCandidateInfo.StrPhoneNo)
                objDataLayer.AddParameter("@Mobile", objCandidateInfo.StrMobile)
                objDataLayer.AddParameter("@Notice_Period", objCandidateInfo.StrNoticePeriod)
                objDataLayer.AddParameter("@Remarks", objCandidateInfo.StrRemarks)
                If (objCandidateInfo.StrYearofExp.Trim() = "") Then
                    objDataLayer.AddParameter("@Years_of_Experience", DBNull.Value)
                Else
                    Dim DecExp As Decimal = -1
                    If (Decimal.TryParse(objCandidateInfo.StrYearofExp, DecExp)) Then
                        objDataLayer.AddParameter("@Years_of_Experience", DecExp)
                    Else
                        objDataLayer.AddParameter("@Years_of_Experience", DBNull.Value)
                    End If
                End If
                objDataLayer.AddParameter("@Address", objCandidateInfo.StrAddress)

                objDataLayer.AddParameter("@Email", objCandidateInfo.StrEmail)
                If (objCandidateInfo.intIndustry > 0) Then
                    objDataLayer.AddParameter("@Industry", objCandidateInfo.intIndustry)
                Else
                    objDataLayer.AddParameter("@Industry", DBNull.Value)
                End If
                If (objCandidateInfo.intFunctionalArea > 0) Then
                    objDataLayer.AddParameter("@Functional_Area", objCandidateInfo.intFunctionalArea)
                Else
                    objDataLayer.AddParameter("@Functional_Area", DBNull.Value)
                End If
                If (objCandidateInfo.StrDob.Trim() = "") Then
                    objDataLayer.AddParameter("@DOB", DBNull.Value)
                Else
                    Try
                        objDataLayer.AddParameter("@DOB", Convert.ToDateTime(objCandidateInfo.StrDob))
                    Catch ex As Exception
                        objDataLayer.AddParameter("@DOB", DBNull.Value)
                    End Try
                End If
                If (objCandidateInfo.intSourceBy > 0) Then
                    objDataLayer.AddParameter("@Source", objCandidateInfo.intSourceBy)
                Else
                    objDataLayer.AddParameter("@Source", DBNull.Value)
                End If
                objDataLayer.AddParameter("@SKILL_SET", objCandidateInfo.strSkillSet)
                objDataLayer.AddParameter("@ATTACHMENT_DATA", objCandidateInfo.ByteData)
                objDataLayer.AddParameter("@EXTENSION", objCandidateInfo.StrExtension)
                objDataLayer.AddParameter("@CREATED_BY", ClsCommons.IntUserId)

                Result = objDataLayer.ExecuteNonQuery("TR_PROC_AddUpdateFullCandidateData")
            Else
                MsgBox("Data is not extracted, Some Error Occured, please update your software.")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return Result
    End Function

I hope I clear my query

我希望我清除我的查询

回答by xxbbcc

(Edited after several changes to question.)

(在对问题进行多次更改后进行了编辑。)

If you only want to get the text content of the file, you need to handle text files and binary files differently. If the input file format is text-base (.txt, .htm, etc.) you can mostly treat it as a string, although you still need to know what encoding to use.

如果只想获取文件的文本内容,则需要对文本文件和二进制文件进行不同的处理。如果输入文件格式是基于文本的(.txt、.htm 等),您通常可以将其视为字符串,尽管您仍然需要知道要使用的编码。

If, however, the input file format is binary (like .doc, .docx, etc.), you cannot just convert your byte array directly to a string because the file contents do not represent only text - the bytes describe layout, formatting, and other information about the file. In that case you need to use Word or some other 3rd-part library to handle the file data for you.

但是,如果输入文件格式是二进制的(如 .doc、.docx 等),则不能将字节数组直接转换为字符串,因为文件内容不仅仅表示文本 - 字节描述布局、格式、以及有关该文件的其他信息。在这种情况下,您需要使用 Word 或其他一些第 3 部分库来为您处理文件数据。

To get the content of a Word document using automation, just create an instance of Word.Application, open a document, select all text in its active window and use the Selection.Textproperty to get the text into a string. Something like:

要使用自动化获取 Word 文档的内容,只需创建 的实例Word.Application,打开文档,选择其活动窗口中的所有文本,然后使用Selection.Text属性将文本转换为字符串。就像是:

oDocument.ActiveWindow.Selection.WholeStory()
sText = oDocument.ActiveWindow.Selection.Text

The Selectionobject is an instance of Rangein Word. This gives you the plain, unformatted content of the document. You can either convert it to a byte array or use it as a string. To convert it to a byte array, you need to use an encoding because in-memory characters must be translated to bytes.

Selection对象是RangeWord 中的一个实例。这为您提供了文档的简单、未格式化的内容。您可以将其转换为字节数组或将其用作字符串。要将其转换为字节数组,您需要使用编码,因为必须将内存中的字符转换为字节。

If you want to convert your content to RTF format, you need 3rd-part tools (or implement the RTF format yourself) - RTF is not a plain text format, it has fairly complex structure.

如果要将内容转换为 RTF 格式,则需要第 3 部分工具(或自己实现 RTF 格式)- RTF 不是纯文本格式,它具有相当复杂的结构。

You can also use Word to save a document in RTF format - look up the Document.SaveAs2()method to do this. This saves the document to disk in RTF format. If you need this data in a database, just read the .rtf file (File.ReadAllBytes()) and then save the bytes to the database.

您还可以使用 Word 以 RTF 格式保存文档 - 查找Document.SaveAs2()执行此操作的方法。这会将文档以 RTF 格式保存到磁盘。如果您需要数据库中的这些数据,只需读取 .rtf 文件 ( File.ReadAllBytes()),然后将字节保存到数据库中。