从十六进制转换回正常并将文件结果保存为OutOfMemory Exception [英] Converting from hex back to normal and saving the file results to OutOfMemory Exception

查看:69
本文介绍了从十六进制转换回正常并将文件结果保存为OutOfMemory Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在vb.net中创建一个程序,它的作用是当你打开一个文件时它会把文件打开成十六进制代码,但问题是如果我保存一个文件是大,它通常导致OutOfMemory异常。

尝试
Dim b As Byte()= RichTextBox1 .Text.Split("" c).Select(Function(n)Convert.ToByte(Convert.ToInt32(n,16)))
My.Computer.FileSystem.WriteAllBytes(SaveFileDialog1.FileName,b,错误)
Catch ex1 As Exception
尝试
Dim b As Byte()= RichTextBox1.Text.Split("" c).Select(Function(n)Convert.ToByte(Convert .ToInt32(n,16)))
My.Computer.FileSystem.WriteAllBytes(OpenFileDialog1.FileName,b,False)
Catch ex As Exception
MsgBox(" Exception caught:" + vbNewLine + vbNewLine + ex.ToString,MsgBoxStyle.Critical," Exception Error")
结束尝试
结束尝试




Lloyd .CH。

解决方案

在尝试转换为字节而不是内存不足异常时,我的代码是InvalidCastException ...


你能否确认你的代码通过了这行

 Dim b As Byte()= RichTextBox1 .Text.Split(QUOT; " c).Select(Function(n)Convert.ToByte(Convert.ToInt32(n,16)))


从你的代码开始,你首先将句子分成带有分割的单词,你得到一个字符串数组,然后你尝试用Convert.ToInt将每个字符串转换成整数...这对大多数"字符串"都不起作用。 ..


看看
msdn:https://msdn.microsoft.com/en-us/library/sf1aw27b%28v=vs.110%29.aspx?f=255&MSPPError = -2147217396


您需要重新评估算法......


我建议转换每个字符(即使是"&"; ; C) 进入十六进制代码。您可以搜索这样的算法,但是这个算法有效......:

函数StringToHex(ByVal text As String)As String 
Dim hex As String
For i As Integer = 0 to text.Length - 1
hex& = Asc(text.Substring(i,1))。ToString(" x")。ToUpper
下一个
返回十六进制
结束功能




I'm trying to make a program in vb.net and what it does is that when you open a file it turns the file opened into hexadecimal code, but the problem is that if I save a file that is large, It usually results in a OutOfMemory Exception.

            Try
        Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16)))
        My.Computer.FileSystem.WriteAllBytes(SaveFileDialog1.FileName, b, False)
    Catch ex1 As Exception
        Try
            Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16)))
            My.Computer.FileSystem.WriteAllBytes(OpenFileDialog1.FileName, b, False)
        Catch ex As Exception
            MsgBox("Exception caught : " + vbNewLine + vbNewLine + ex.ToString, MsgBoxStyle.Critical, "Exception Error")
    End Try
    End Try


Lloyd .CH.

解决方案

What I get with your code is a InvalidCastException when trying to convert to byte, not a Out of Memory exception...

Could you confirm that your code pass the line

Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16)))

From your code, first your are splitting sentences into words with the split, you get an array of string, then you try to convert each string into integer with Convert.ToInt... that won't work for most "string"...

Look at the msdn on Convert.ToInt: https://msdn.microsoft.com/en-us/library/sf1aw27b%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

You need to reevaluate your algorithm...

I suggest to convert each character (even the " "c)  into their hex code. You can search for such an algorithm, but this one works...:

Function StringToHex(ByVal text As String) As String
        Dim hex As String
        For i As Integer = 0 To text.Length - 1
            hex &= Asc(text.Substring(i, 1)).ToString("x").ToUpper
        Next
        Return hex
    End Function



这篇关于从十六进制转换回正常并将文件结果保存为OutOfMemory Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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