happysmile
happysmile

Reputation: 7777

Assign value to type byte[] in c#

How can I set a value to a byte[]

I have tried the following but I get a compilation error:

byte[] XMLbyte=null;
XMLbyte = byte(2345)

Any suggestions how I can do this?

Upvotes: 0

Views: 26414

Answers (7)

Jignesh
Jignesh

Reputation: 310

Try this: byte[] temp = new byte [255];

Or this: byte[] temp = new byte [123];

Upvotes: 0

Yodan Tauber
Yodan Tauber

Reputation: 3967

Some options to initialize an array:

XMLbyte = new byte[50]; // creates an array with 50 elements, all values are zero.
XMLbyte = new byte[3] {1,2,3}; // creates an array with 3 elements: 1,2,3
XMLbyte = new byte[] {1,2,3}; // creates an array with 3 elements: 1,2,3
XMLbyte = {1,2,3}; // creates an array with 3 elements: 1,2,3

Upvotes: 1

pinichi
pinichi

Reputation: 2205

You must XMLbyte = new byte[2345];

maybe you want a bytearray from "2345" string ?

System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
XMLbyte = encoding.GetBytes("2345");

Upvotes: 4

TalentTuner
TalentTuner

Reputation: 17556

try below code

byte[] XMLbyte=null;  
 XMLbyte = new byte[2] {1,2}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

You are looking for the GetBytes method:

byte[] XMLbyte = BitConverter.GetBytes(2345);

Upvotes: 0

sirus
sirus

Reputation: 1599

Well, it seems silly but I think you are missing the new operator

XMLbyte = new byte[2345]

Upvotes: 0

DGH
DGH

Reputation: 11539

byte[] is an array of bytes. You might want just the byte type.

Upvotes: 0

Related Questions