how to exchange 1234(number) -> bytes sequence

  • Thread starter Thread starter penzer
  • Start date Start date
penzer said:
how to exchange 1234(number) -> bytes sequence in the C#?

What do you mean by "bytes sequence"?

Do you mean take the 32-bit integer value 1234 and change it to an
array of 4 bytes, each containing 8 of the bits of the original 32-bit
value?

Or do you mean formatting the integer value 1234 as a printable string?
(This one is easy: just call ToString() on the value, or use
String.Format("{0}", 1234);)

Or do you mean something else?

It would help if you would describe the expected output in more detail
than simply "bytes sequence".
 
I mean I want to encode 32-bit integer value to byte sequence.

example,
char [] chars = "abcd"
int iBC = enc.GetByteCount( chars );

but i want to make interger value to byte[].
 
CLR char types are 16 bit, so your example is duff.

It would *really* help your example if you told us the expected outcome.
People have indicated that your question is vague: it really is!

So: do you mean the **string** "1234" as a byte[] of the (encoded) character
data (Encoding)? Or perhaps you mean the bytes that make up an integer
(BitConverter); anyways - do these help?

static void WriteHex(string caption, byte[] data)
{
StringBuilder sb = new StringBuilder();
foreach(byte b in data) { // possibly backwards; quick demo only
sb.Append(b.ToString("x2"));
}
Debug.WriteLine(sb, caption);
}
static void Main()
{
WriteHex("Encoding", Encoding.UTF8.GetBytes("1234"));
WriteHex("BitConverter", BitConverter.GetBytes(1234));

}

Output:
Encoding: 31323334
BitConverter: d2040000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top