Results 1 to 10 of 10

Thread: [2005] Byte to byte array

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    53

    [2005] Byte to byte array

    Well I got a bunch of byte and I was wondering if there was any easy to convert them to a byte array? Lets say I have "00025525525525525525525525581562000025525525525510097109106815620000255255255255115121104112815 620" would I be able to make that an array?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Byte to byte array

    Would that line of bytes initially be stored in a string?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    53

    Re: [2005] Byte to byte array

    It was originally a string yes.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Byte to byte array

    This would be one way. Sure there are plenty of ways.

    VB Code:
    1. Dim byteString As String = "05435206601341209852439058"
    2.         Dim byteArray(byteString.Length - 1) As Byte
    3.         For i As Integer = 0 To byteString.Length - 1
    4.             If Not Byte.TryParse(byteString(i), byteArray(i)) Then
    5.                 MessageBox.Show("The string containing bytes contained an unvalid value")
    6.             End If
    7.         Next
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    53

    Re: [2005] Byte to byte array

    They aren't being written properly. I got an array of bytes, converted each byte into a string which got me those numbers, then when I write them to the file it isn't writing them to the same as they were firstly. Thats the code I'm using to write the bytes.

    Code:
    System.IO.File.WriteAllBytes(textbox1.Text, byteArray)
    I used this code to retreive the bytes.

    Code:
    byte_array1 = System.IO.File.ReadAllBytes(directory_textbox.text)
            For Each bte As Byte In byte_array1
               TextBox2.AppendText(CStr(bte))
            Next
    Last edited by skylinegtr13; Jul 11th, 2007 at 09:47 PM.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Byte to byte array

    byte_array1 = system.text.encoding.ASCII.GetBytes(directory_textbox.text)

    Now, having done that, what you are doing in the loop is not going to get you close to what you want. If the byte holds 123, then it will convert to the string "123", which is three characters. When you then try to convert that back to bytes, you end up with three bytes ASC("1"), ASC("2"), and ASC("3"), when what you really want is a single byte holding 123.

    Unfortunately, there is no really sound way to get that 123 back, unless you pad all values to three characters (e.g. byte value 5 becomes string "005"), which you will have to do yourself.

    What is the ultimate goal of converting to strings, concatenating them together, then disassembling them again?
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    53

    Re: [2005] Byte to byte array

    Well as you can see the file holds a value of "0" and when you convert it to text it will just make the "0" a space which is not "0". I want to add to the file without changing all those value to something that it shouldn't be which would make the file useless for its purpose.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Byte to byte array

    You mean the initial three bytes being "000"? The next 8 trios of characters are all "255", but the trio after that is too high to fit in a byte. That's what I meant about needing to pad out the value with zeroes.

    However, if you want to add to the file, do you have to add in the middle, or only at the end?
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    53

    Re: [2005] Byte to byte array

    Well I just want to change the middle. I don't want to change it but I want to create a new file. I only got the array of bytes so I could get the information that doesn't change and then have the program create the text which does change and then create a new file with the new information the program added so it doesn't have to overwrite the existing file which might be needed.

    Edit: Well I made it easier by adding textbox1.paste(" ") so I can make out each byte. So would I use that same code to compile them back?
    Code:
    Dim byteArray(byteString.Length - 1) As Byte        For i As Integer = 0 To byteString.Length - 1
    Last edited by skylinegtr13; Jul 12th, 2007 at 06:45 AM.

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    53

    Re: [2005] Byte to byte array

    I still can't seem to get to work any more help?

    Edit:

    I got it to work by using:

    Code:
    Bytearray1.setvalue() ; Array.resize()
    Last edited by skylinegtr13; Jul 12th, 2007 at 11:33 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width