C
C#3y ago
koopa

Not sure how the byte[] works in Csharp

Hi, basically I have a very simple program which I'm using to try and understand how bytes work:
namespace Encryptor
{
class Program
{

static void Main(string[] args)
{
string plainText = "Test";
byte[] plainBytes;
plainBytes = ObjectToByteArray(plainText);
foreach(int i in plainBytes)
{
Console.WriteLine(i);
}
}
public static byte[] ObjectToByteArray(Object obj)
{

BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
}

}
namespace Encryptor
{
class Program
{

static void Main(string[] args)
{
string plainText = "Test";
byte[] plainBytes;
plainBytes = ObjectToByteArray(plainText);
foreach(int i in plainBytes)
{
Console.WriteLine(i);
}
}
public static byte[] ObjectToByteArray(Object obj)
{

BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
}

}
It is my understanding that the integers printed should be the 4 ascii values for "Test" and nothing more, instead this is printed (grouped into sets of 4 for easier reading):
0 1 0 0 0 255 255 255 255 1 0 0 0 0 0 0 0 6 1 0 0 0 4 84 101 115 116 11
0 1 0 0 0 255 255 255 255 1 0 0 0 0 0 0 0 6 1 0 0 0 4 84 101 115 116 11
And I am unsure as to why, could anyone be of assistance please
5 Replies
mtreit
mtreit3y ago
First of all, don't use BinaryFormatter for anything. Second of all, BinaryFormatter is not doing a direct conversion to bytes, it has its own format. To convert text to bytes use the appropriate Encoding class
MODiX
MODiX3y ago
mtreit#6470
REPL Result: Success
var plainText = "Test";
var plainBytes = Encoding.ASCII.GetBytes(plainText);

foreach (var b in plainBytes)
{
Console.WriteLine(b);
}
var plainText = "Test";
var plainBytes = Encoding.ASCII.GetBytes(plainText);

foreach (var b in plainBytes)
{
Console.WriteLine(b);
}
Console Output
84
101
115
116
84
101
115
116
Compile: 568.476ms | Execution: 44.608ms | React with ❌ to remove this embed.
koopa
koopaOP3y ago
Ah okay this makes sense - is there any way of directly converting any data structure to bytes or will it be in its own format like bf?
mtreit
mtreit3y ago
For simple structs that only contain primitive types you can come up with a pure binary representation but once you start including references to other types that goes out the window. For instance, a type that contains a field that is an array of strings: in memory that field is an address (a pointer) that points to an array. Every element of the array is itself a pointer, each of which points to a string instance. You can't really directly convert such a thing to a simple array of bytes because of the indirection caused by having pointers. This is why we use serializers to convert complex types to some format that can be written to disk or sent over the network - often this is a textual format (think JSON) but binary formats are also possible (think ProtoBuf). BinaryFormatter has its own format but it has huge security holes and is considered deprecated. Don't use it.
koopa
koopaOP3y ago
Sure thank you very much for the help - I'll look for alternatives to binary formatter
Want results from more Discord servers?
Add your server