[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?
Re: [2005] Byte to byte array
Would that line of bytes initially be stored in a string?
Re: [2005] Byte to byte array
It was originally a string yes.
Re: [2005] Byte to byte array
This would be one way. Sure there are plenty of ways.
VB Code:
Dim byteString As String = "05435206601341209852439058"
Dim byteArray(byteString.Length - 1) As Byte
For i As Integer = 0 To byteString.Length - 1
If Not Byte.TryParse(byteString(i), byteArray(i)) Then
MessageBox.Show("The string containing bytes contained an unvalid value")
End If
Next
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
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?
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.
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?
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
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()