无法将类型为"System.String"的对象转换为类型为"System.Byte []"的对象.在VB.Net [英] Unable to cast object of type 'System.String' to type 'System.Byte[]'. in VB.Net

查看:273
本文介绍了无法将类型为"System.String"的对象转换为类型为"System.Byte []"的对象.在VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在下面的代码中遇到了上面提到的错误.
错误发生在byteMap = varMapData(lCount)部分.

Hi,
I''ve got an error stated above in my below coding.
The error occurred at byteMap = varMapData(lCount) section.

Sub cmdGetHistoryByStrip_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim objFactory As Object
        Dim objSEPI As Object
        Dim varReturnResult As Object
        Dim varMapCount As Object
        Dim varMapData As Object
        Dim byteMap() As Byte
        Dim sStatus As String = ""
        Dim strStripID As String = ""
        Dim lCount As Integer
        

        varReturnResult = objSEPI("PMASFT", strStripID)
        sStatus = varReturnResult(0)

            'If XML reply "SUCCESS" then write the binary file to local HDD.
            varMapCount = varReturnResult(1)
            varMapData = varReturnResult(2)

'Split and save the ind map
        For lCOunt = 0 To UBound(varMapData)
            'Get the map data from array.
            ReDim byteMap(Len(varMapData(lCount)))
            byteMap = varMapData(lCount)
            Dim lFileNum As Integer
            Dim sFileName As String = "C:\SEPI_SM\" & strStripID & ".SM"
            FileOpen(lFileNum, sFileName, OpenMode.Binary)
            FilePut(lFileNum, byteMap, )
            FileClose(lFileNum)
        Next


请帮忙解决这里的问题.感谢您的帮助.


Please help out what is wrong here. Appreciate ur help.

推荐答案

这些类型无关.难怪,这种分配是错误的.如果我知道byteMap的目的,我会告诉您如何获得其价值.

首先,字符不是字节,而是Unicode字符.在内部,该字符串使用Unicode UTF-16表示.在此编码中,字符是16位单词或一对这样的单词(称为代理对" ).当然,可以将字符串序列化为字节数组,但是结果取决于您选择的编码.

例如,System.Text.Encoding.UTF8.GetBytes(string)将返回UTF-8中的字节数组.顺便说一句,如果所有字符都落在ASCII范围内,则此结果将与ASCII完全相同. UTF-8是面向字节的UTF,其可变大小的字节序列表示一个字符.

请参阅:
http://msdn.microsoft.com/en-us/library/system.text. encoding.aspx [^ ],
http://unicode.org/ [ ^ ],
http://unicode.org/faq/utf_bom.html [
These types are unrelated; no wonder, this assignment is wrong. If I knew the purpose of byteMap I would tell you how to get its value.

First of all, characters are not bytes, they are Unicode characters. Internally, the string is represented using Unicode UTF-16. In this encoding, a character is a 16-bit word or a pair of such words (called surrogate pair). Of course, a string can be serialized into an array of bytes, but the result depends on the encoding you choose.

For example, System.Text.Encoding.UTF8.GetBytes(string) will return array of bytes in UTF-8. By the way, this result would be the exactly the same as ASCII if all characters happen to fall into the ASCII range. UTF-8 is a byte-oriented UTF with variable size of a byte sequence expressing one character.

Please see:
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^],
http://unicode.org/[^],
http://unicode.org/faq/utf_bom.html[^].

—SA


很抱歉,因为我是新来的.现在,我想将此varMapData(对象)转换为字符串数组.我尝试使用DirectCast和CType,但无法正常工作.我遇到此错误消息:无法将类型为"System.Object []"的对象强制转换为类型为"System.String".我想将其转换为字符串数组,并将数据保存到平面文件中.

将varMapData变暗为对象
昏暗的StrMap()作为字符串

StrMap = DirectCast(varMapData,String)



预先感谢.
Sorry for the hassle because I''m new here. Now I want to convert this varMapData (object) into array of string. I''ve tried to use DirectCast and CType but couldn''t work. I encountered this error message: Unable to cast object of type ''System.Object[]'' to type ''System.String''. I want to convert it to array of string and save the data into a flat file.

Dim varMapData As Object
Dim StrMap() As String

StrMap = DirectCast(varMapData, String)



Thanks in advance.


这篇关于无法将类型为"System.String"的对象转换为类型为"System.Byte []"的对象.在VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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