asp.net 图片和十六进制互转

asp.net(vb)图片转十六进制字符串,且保存在文本文件中

代码:

        Dim fs As FileStream = Nothing
        Dim br As BinaryReader = Nothing
        Dim sw As StreamWriter = Nothing
        Try
            fs = New FileStream("11.jpg", FileMode.Open, FileAccess.Read)
            br = New BinaryReader(fs)
            sw = New StreamWriter("11.txt")
            Dim length As Int16 = fs.Length
            While length > 0
                Dim tempbyte As Byte = br.ReadByte
                Dim tempint As Int16 = Convert.ToInt32(tempbyte)
                Dim tempstr As String = Convert.ToString(tempint, 16)
                sw.WriteLine(tempstr)
                length = length - 1

            End While
            MsgBox("完成")
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            sw.Close()
            br.Close()
            fs.Close()
        End Try

 

从记事本中读取十六进制,还原成图片

       Dim fs As FileStream = Nothing
        Dim bw As BinaryWriter = Nothing
        Dim sr As StreamReader = Nothing
        Try
            fs = New FileStream("22.jpg", FileMode.Create, FileAccess.Write)
            bw = New BinaryWriter(fs)
            sr = New StreamReader("11.txt")
            While sr.Peek <> -1
                Dim tempstr As String = sr.ReadLine
                Dim tempint As Int16 = Convert.ToInt16(tempstr, 16)
                Dim tempbyte As Byte = Convert.ToByte(tempint)
                bw.Write(tempbyte)

            End While
            MsgBox("ok")
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            sr.Close()
            bw.Close()
            fs.Close()
        End Try

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值