Inserting large numbers into a byte array

I have been dealing with this problem for months now and have basically been avoiding it.

I have a program that can load a file into a byte array using the following:

Public Class Form1
Protected b() As Byte

Private Sub LoadFile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LoadFile.Click

Try
Dim open As New OpenFileDialog()

With open
.CheckFileExists() = True
.CheckPathExists() = True
.Filter() = "All Files (*.*)|*.*"
.FilterIndex() = 0
.Multiselect() = False
.SupportMultiDottedExtensions() = True
.Title() = "Open File"
End With

If open.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub 'If not clicked then exit the sub
b = My.Computer.FileSystem.ReadAllBytes(open.FileName)

Catch ex As Exception
MsgBox(ex.Message)

End Try

End Sub 'LoadFile


Private Sub SaveFile(ByVal sender As System.Object, ByVal e As System.EventArgs)

Try
Dim save As New SaveFileDialog()

With save
.CheckPathExists() = True
.CreatePrompt() = True
.Filter() = "All Files (*.*)|*.*"
.FilterIndex() = 0
.OverwritePrompt() = True
End With


If save.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
My.Computer.FileSystem.WriteAllBytes(save.FileName, b, False)

Catch ex As Exception
MsgBox(ex.Message)

End Try

End Sub 'SaveFile

Private Sub TextBox_Offset_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox_Offset.TextChanged
Try
'When you change the offset box text, update the value box text
TextBox_8bit.Text = CStr(b(CInt(TextBox_Offset.Text)))
TextBox_16bit.Text = CStr(BitConverter.ToInt16(b, CInt(TextBox_Offset.Text)))
TextBox_32bit.Text = CStr(BitConverter.ToUInt32(b, CInt(TextBox_Offset.Text)))

Catch ex As Exception
MsgBox("Offset does not exist in the current file.")
'Now set the offset box to 0
TextBox_Offset.Text = CStr(0)

End Try
End Sub 'TextBox_Offset_TextChanged

End Class

Now, what that does is load a file into the byte array, b. When you enter a value into the offset textbox it displays the value at that offset in three other textboxes in 8bit, 16bit, and 32bit formats.

What I cannot figure out is how to save the 16bit and 32bit values back to the byte array when they are changed. I know that one can only enter 8bit values into a byte array at a time.

This is what I have tried:
b(CInt(TextBox_Offset.Text)) = CByte(CLng(TextBox_32bit.Text) Or CLng(TextBox_32bit.Text) >> 8 Or CLng(TextBox_32bit.Text) >> 16)

Now, before you say it, I know it is wrong. It gives an arithmetic overflow error. I have previously used a sub that was given to me to do it with 16bit numbers. It follows:

Private Sub ConvertToBytes(ByVal int As Integer, ByVal bA As Byte(), ByVal Offset As Integer)

Dim t As Integer = int
bA(Offset) = t
And &HFF
t = t >> 8
If t = 0 Then Exit Sub
bA(Offset + 1) = t
And &HFF

End Sub

I don't want to use that anymore though. I want to be able to do it in one line of code, like I tried to above. I remember doing it before but I have since deleted that project and cannot remember what I did. Any help would be greatly appreciated. Also, I can send my project to you if it may help.

Enjoy!




Answer this question

Inserting large numbers into a byte array

  • S Dano

    I an others have been working with you for months on this.

    Lets try datatypes.

    There are Bytes which are eight bits wide and you can never get more than eight bits in them. A byte is unsigned. It can hold a number as large as 255.

    |IIIIIIII|

    A Short is 16 bits wide and may be signed or unsigned. A signed short can express numbers between +32767 and - 32767. An unsigned short - 65535. A short is two bytes contiguous in memory.

    |IIIIIIII|IIIIIIII| <------ a short

    An Integer is 32 bits wide and may be either signed or unsigned.It may be thought of as four contiguous bytes in memory.

    |IIIIIIII|IIIIIIII|IIIIIIII|IIIIIIII| < -------- An integer in memory

    Conversion of these datatypes is really easy

    Lets say you have a stream of four bytes in an array you want to fill with the contents of an integer

    | 0 | | 1 | | 2 | | 3 | four bytes in memory

    The object of the game move the bytes of the integer into the byte array.

    |IIIIIIII|IIIIIIII|IIIIIIII|IIIIIIII|
    | | | |
    | | | +-------------+
    | | +--------+ |
    V +---+ | |
    | | |
    V V V
    | 0 | | 1 | | 2 | | 3 |

    Code to accomplish this would look exactly like this:

    Dim Length As UInt32 = a.Length - 4

    a(3) = Length And &HFF ' Store the
    a(2) = Length >> 8 And &HFF ' bytecount
    a(1) = Length >> 16 And &HFF ' in first
    a(0) = Length >> 24 And &HFF ' four bytes

    This is real code.

    These four lines fill a four byte array from an Integer.



  • msdn_haii

    Ok, that isn't what I meant. I just didn't explain it enough. I know that a byte is 8 bits. This is what I meant:

    I have a byte buffer, b, that hold the following array of bytes: 00, 12, 36, 52, 87, 45, 14, 25, 65, 00, 00, 19

    Now, I want to insert a 32bit value into that array at offset 4. The 32bit value is 55789665h

    So now the array will look like this: 00, 12, 36, 52, 55, 78, 96, 66, 65, 00, 00, 19

    I didn't want to cram it into a single byte, I wanted to cram it into the array, and since it's 4 bytes the second, third and fourth byte will overflow into the other offsets, like I have shown above.

    Now, back to my original question. Is there an easier way to do this than the way you have outlined

    Thank you.



  • sss5234

    Renee"There are Bytes which are eight bits wide and you can never get more than eight bits in them. "

    Troy: Store the value of the textbox in the byte array b, but store it as a 16 (or 32 or 64 or whatever) bit value.

    Troy,

    For the last four months I've been trying to tell you... that a byte will not hold more than eight bits.

    In a byte array, it is impossible to store more than eightbits in a byte - anytime, anyplace.



  • Jonathan MacCollum

    This is as simple as you will ever get....

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim b(42) As Byte

    Fillbytes(b, 9000, 0)

    End Sub

    Private Sub Fillbytes(ByRef bArray() As Byte, ByRef IntVal As Integer, ByVal offset As Integer)

    Dim bAr(3) As Byte
    bAr = System.BitConverter.GetBytes(IntVal)
    bArray(offset) = bAr(0)
    bArray(offset + 1) = bAr(1)
    bArray(offset + 2) = bAr(2)
    bArray(offset + 3) = bAr(3)

    End Sub



  • Shankar KC

    I understand all that, really I do. What I wanted was a way to do it without using that method. Without assigning four different variables (or an array). Basically a way for me to say:

    Store the value of the textbox in the byte array b, but store it as a 16 (or 32 or 64 or whatever) bit value.

    I have the concept of that, the code you wrote. I just believe there must be a better way to do it.
    I find it hard to believe that it is so easy to extract a 32bit value from an array (BitConverter.ToUInt32), but it is so much more difficult to insert one back. And I use difficult loosely as you may think it isn't difficult.

    Basically, there has to be a better way to do this.

    Thanks.
    Enjoy!

    And I know you and others have been trying to help me. I guess I am having a hard time grasping this.


  • Inserting large numbers into a byte array