Jump to content

Copying a value from one byte array to another

Shammikit

so i have a byte array called data that gets values dynamically. i want to copy a value in a specific position from the array data to the other array named bytes.i have tried the code below and it gives me the error : An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code. Additional information: Source array was not long enough. Check srcIndex and length, and the array's lower bounds. what am i doing wrong?

byte[] bytes = new byte[data.Length];                  
Array.Copy(data, 0, bytes, 0, 1);
textBox1.Text = Encoding.Default.GetString(bytes);

 

Link to comment
Share on other sites

Link to post
Share on other sites

Can't tell which language you are using but if I understand correctly, you just want to copy a specific byte from one position of an array to another position of another array. have you tried just doing something like:
 

data[position] = bytes[position];

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, ryan27968 said:

Can't tell which language you are using but if I understand correctly, you just want to copy a specific byte from one position of an array to another position of another array. have you tried just doing something like:
 


data[position] = bytes[position];

 

i did but i got this error:

An exception of type 'System.IndexOutOfRangeException' occurred in Server.exe but was not handled in user code

Additional information: Index was outside the bounds of the array.

 

im using C# by the way.forgot to mention that

Link to comment
Share on other sites

Link to post
Share on other sites

Well then clearly you are trying to read from a part of the array that doesn't exist, or you are trying to put data into a part of the array that doesn't exist. 

Haven't done c# before so i'm just guessing here but I'd personally just 

byte[data.Length] bytes;

instead of what you are doing

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, ryan27968 said:

Well then clearly you are trying to read from a part of the array that doesn't exist, or you are trying to put data into a part of the array that doesn't exist. 

Haven't done c# before so i'm just guessing here but I'd personally just 


byte[data.Length] bytes;

instead of what you are doing

is this: byte[data.Length] bytes;

the same as this: byte[] bytes = new byte[data.Length];

 

if yes,that is what i tried previously when i tried to initialize it like this data[position] = bytes[position];

Link to comment
Share on other sites

Link to post
Share on other sites

47 minutes ago, Shammikit said:

so i have a byte array called data that gets values dynamically. i want to copy a value in a specific position from the array data to the other array named bytes.i have tried the code below and it gives me the error : An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code. Additional information: Source array was not long enough. Check srcIndex and length, and the array's lower bounds. what am i doing wrong?


byte[] bytes = new byte[data.Length];                  
Array.Copy(data, 0, bytes, 0, 1);
textBox1.Text = Encoding.Default.GetString(bytes);

 

You're getting that error because data is empty

// Example
byte[] data = {}; // empty
byte[] bytes = new byte[data.Length];
Array.Copy(data, 0, bytes, 0, 1); // Throws ArgumentException: Source array was not long enough. Check srcIndex and length, and the array's lower bounds.

// If data has at least 1 item, that code works
byte[] data = {255}; // 1 element
byte[] bytes = new byte[data.Length];
Array.Copy(data, 0, bytes, 0, 1); // works

Note that Array.Copy is probably overkill for a single item though.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×