1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Problem Saving Hex Numbers to a File

Discussion in 'Software Development' started by FausseFugue, Jan 5, 2010.

Thread Status:
Not open for further replies.
  1. FausseFugue

    FausseFugue Thread Starter

    Mar 23, 2009
    I'm currently writing a software in Visual Basic 2008 Express Edition and I need to save a file as hex code.

    It's all working, until I get to the part where I have hex numbers to save to the file. For some reason some numbers are converting properly but some others are not.

    Here's my code to save the numbers:
    Private Sub Menu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim mySaveFileDialog As New SaveFileDialog
       Dim enUnicode As System.Text.Encoding = System.Text.Encoding.Unicode
       If mySaveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
          Dim destinationFileWriter As New System.IO.FileStream(mySaveFileDialog.FileName, IO.FileMode.Create)
          If (destinationFileWriter IsNot Nothing) Then
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("126"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("127"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("128"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("129"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("130"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("131"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("132"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("133"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("134"))), 0, 1)
             destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("135"))), 0, 1)
          End If
       End If
    End Sub
    Public Function hex2ascii(ByVal hextext As String) As String
       Dim num
       Dim Value As String = ""
       For y = 1 To Len(hextext)
          num = Mid(hextext, y, 2)
          Value = Value & Chr(Val("&h" & num))
          y = y + 1
       Next y
       hex2ascii = Value
    End Function
    (This isn't the actual code I'm using, mine is taking numbers from an xml file instead of having the numbers written directly in the code, but the result is the same.)

    The resulting file's hex values should be:
    7e 7f 80 81 82 83 84 85 86 87
    but instead it's:
    7e 7f ac 81 1a 92 1e 26 20 21

    Would someone have an idea why it's doing this? For most numbers it's working properly, there's only a couple of numbers that don't work properly.

  2. FausseFugue

    FausseFugue Thread Starter

    Mar 23, 2009
    Ok, I think I found a way to do it. I was doing it all wrong.

    What I had to do was change this:
    destinationFileWriter.Write(enUnicode.GetBytes(hex2ascii(Hex("134"))), 0, 1)
    to this:

    So far it seems to be working!
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Similar Threads - Problem Saving Numbers
  1. mohittomar13
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/891266

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice