Hack Forums
[C#] [C#] Simple function to merge byte arrays - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [C#] Simple function to merge byte arrays (/showthread.php?tid=702853)



[C#] Simple function to merge byte arrays - ubbelol - Sep 26, 2010

Very simple function I made to "merge" or append multiple byte arrays, can't find the word for it.

Code
private byte[] mergeBytes(byte[][] inputBytes)
  {
    int tmp = 0;
    byte[] output;

    foreach (byte[] bArr in inputBytes){
    tmp += bArr.Length;
    }

    output = new byte[tmp]; tmp = 0;

    foreach (byte[] bArr in inputBytes){
    Buffer.BlockCopy(bArr, 0, output, tmp, bArr.Length); tmp += bArr.Length;
    }

    return output;
  }

Example usage:

Code
byte[] b = new byte[7]; b = Encoding.Default.GetBytes("UbbeLoL");
    byte[] b2 = new byte[3]; b2 = Encoding.Default.GetBytes("123");
    string str = Encoding.Default.GetString(mergeBytes(new byte[][] { b, b2 })); //<- str = UbbeLoL123



* - pr0t0typ3 - Sep 26, 2010

lol when I read the thread title I thought of some sort of compression ^^ nice function anyway I always used array.copy before


* - ubbelol - Sep 26, 2010

(Sep 26, 2010 10:53 AM)pr0t0typ3 Wrote:  lol when I read the thread title I thought of some sort of compression ^^ nice function anyway I always used array.copy before

http://danielvl.blogspot.com/2004/04/use-bufferblockcopy-instead-of.html :)


* - iDope -.- - Sep 26, 2010

Can you add [C#] in the Title?
It becomes a bit misleading.
:/


* - ubbelol - Sep 26, 2010

(Sep 26, 2010 11:06 AM)iCoreā„¢ Wrote:  Can you add [C#] in the Title?
It becomes a bit misleading.
:/

I thought it don't make any difference if it's C# or vb.net as this code isn't really hard to convert just run it through an online code convert or something.

But I guess i'll change the title though if it's really that "misleading".


* - iDope -.- - Sep 26, 2010

Not that it ain't that simple to conbvert the code, but yeah, still..It's better to be clear..
:)
Nice share nevertheless.