Byte Array

S

shapper

Hello,

I am writing some Byte arrays to an XElement to save on a XML file.
In some cases this byte array might contain a small image.

I am doing it as follows:

XElement node = new XElement("Node",
new XElement("ByteTest1", Convert.ToBase64String(new Byte[]
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })),
new XElement("ByteTest2", new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9 })
);
Console.WriteLine(node);

I get the following:

<Node>
<ByteTest1>AAECAwQFBgcICQ==</ByteTest1>
<ByteTest2>0123456789</ByteTest2>
</Node>

Should I use Convert.ToBase64String? In which case shouldn't I use it?
And if I should always use it is it possible, or it makes sense, to
create a XElement extension that when the value is of type Byte[] it
uses Base64String?

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
Hello,

I am writing some Byte arrays to an XElement to save on a XML file.
In some cases this byte array might contain a small image.

I am doing it as follows:

XElement node = new XElement("Node",
new XElement("ByteTest1", Convert.ToBase64String(new Byte[]
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })),
new XElement("ByteTest2", new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9 })
);
Console.WriteLine(node);

I get the following:

<Node>
<ByteTest1>AAECAwQFBgcICQ==</ByteTest1>
<ByteTest2>0123456789</ByteTest2>
</Node>

Should I use Convert.ToBase64String? In which case shouldn't I use it?
And if I should always use it is it possible, or it makes sense, to
create a XElement extension that when the value is of type Byte[] it
uses Base64String?

You will always want to use Base64. Stop and think about that
"ByteTest2": assuming your array will ever have some byte value in it
other than zero through nine, how are you going to reverse the encoding?
That is, how will you read the text in the XML element and convert it
back to a byte[]?

Pete
 
J

j1mb0jay

Peter said:
shapper said:
Hello,

I am writing some Byte arrays to an XElement to save on a XML file.
In some cases this byte array might contain a small image.

I am doing it as follows:

XElement node = new XElement("Node",
new XElement("ByteTest1", Convert.ToBase64String(new Byte[]
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })),
new XElement("ByteTest2", new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9 })
);
Console.WriteLine(node);

I get the following:

<Node>
<ByteTest1>AAECAwQFBgcICQ==</ByteTest1>
<ByteTest2>0123456789</ByteTest2>
</Node>

Should I use Convert.ToBase64String? In which case shouldn't I use it?
And if I should always use it is it possible, or it makes sense, to
create a XElement extension that when the value is of type Byte[] it
uses Base64String?

You will always want to use Base64. Stop and think about that
"ByteTest2": assuming your array will ever have some byte value in it
other than zero through nine, how are you going to reverse the encoding?
That is, how will you read the text in the XML element and convert it
back to a byte[]?

Pete

It can be done several years ago i wrote a simple class for doing just this.

using System;
using System.Text;

using JJMath.Big;

namespace JJSecurity.Encryption.RSA
{
class Blocking
{
private BigInteger[] blocks;
private int blockSize = -1;
private String plainText;

public BigInteger[] Blocks { get { return this.blocks; } }
public String PlainText { get { return this.plainText; } }

public Blocking(String toBlock, int blockSize)
{
this.blockSize = (blockSize / 2);
this.GenerateBlocks(this.GenerateByteString(toBlock));
}

public Blocking(String toUnBlock)
{
try
{
this.GenerateCharacterStringFromByteString(toUnBlock);
}
catch
{
throw new Exception("Unable to Create Character String
From Byte String");
}
}

private void GenerateBlocks(String toEncrypt)
{
if (toEncrypt.Length > this.blockSize)
{
int division = (toEncrypt.Length / this.blockSize);
int mod = (toEncrypt.Length % this.blockSize);
BigInteger[] blocks;
if (mod != 0)
blocks = new BigInteger[(division + 1)];
else
blocks = new BigInteger[division];

int blockSize = (toEncrypt.Length / blocks.Length);

int index = 0;
Random random = new Random();
for (int i = 0; i < blocks.Length; i++)
{
if ((i == division) && (mod != 0))
{
blocks = new BigInteger(random.Next(1,
9).ToString() + toEncrypt.Substring(index), 10);
}
else
{
blocks = new BigInteger(random.Next(1,
9).ToString() + toEncrypt.Substring(index, blockSize), 10);
}
index += blockSize;
}
this.blocks = blocks;
}
else
{
this.blocks = new BigInteger[] { new BigInteger(new
Random().Next(1, 9).ToString() + toEncrypt, 10) };
}
}

private String GenerateByteString(String toEncrypt)
{
byte[] bytes = Encoding.UTF8.GetBytes(toEncrypt);
StringBuilder sb = new StringBuilder((toEncrypt.Length * 3));
for (int i = 0; i < bytes.Length; i++)
{
switch (bytes.ToString().Length)
{
case 1:
sb.Append("00");
sb.Append(bytes);
break;
case 2:
sb.Append("0");
sb.Append(bytes);
break;
case 3:
sb.Append(bytes);
break;

}
}
return sb.ToString();
}

private void GenerateCharacterStringFromByteString(String toChange)
{
byte[] bytes = new byte[toChange.Length / 3];
int charIndex = 0;
for (int i = 0; i < bytes.Length; i++)
{
String bytePart = toChange[charIndex].ToString() +
toChange[(charIndex + 1)].ToString() +
toChange[(charIndex + 2)].ToString();
bytes = Byte.Parse(bytePart);
charIndex += 3;
}
this.plainText = Encoding.UTF8.GetString(bytes);
}
}
}


Hope this helps.
 
J

j1mb0jay

Peter said:
shapper said:
Hello,

I am writing some Byte arrays to an XElement to save on a XML file.
In some cases this byte array might contain a small image.

I am doing it as follows:

XElement node = new XElement("Node",
new XElement("ByteTest1", Convert.ToBase64String(new Byte[]
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })),
new XElement("ByteTest2", new Byte[] { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9 })
);
Console.WriteLine(node);

I get the following:

<Node>
<ByteTest1>AAECAwQFBgcICQ==</ByteTest1>
<ByteTest2>0123456789</ByteTest2>
</Node>

Should I use Convert.ToBase64String? In which case shouldn't I use it?
And if I should always use it is it possible, or it makes sense, to
create a XElement extension that when the value is of type Byte[] it
uses Base64String?

You will always want to use Base64. Stop and think about that
"ByteTest2": assuming your array will ever have some byte value in it
other than zero through nine, how are you going to reverse the encoding?
That is, how will you read the text in the XML element and convert it
back to a byte[]?

Pete

A byte as a string can range from the value "000" to "255" this will
always give a string of three characters. if you join all of the bytes
together as strings then you could convert them back into byte array. A
check must be done to make sure that the length of the string MOD 3 is
equal to 0 as you may loose the starting "0" if you are trying to store
them as a number.
 
P

Peter Duniho

j1mb0jay said:
[...] That is, how will you read the text in the XML element and
convert it back to a byte[]?

It can be done

No, it can't. My question was rhetorical. Using the text format shown
in the original post, it is not possible to distinguish digits from
individual bytes in the string. The value "12" looks just like the
values "1" and "2".
 

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

Similar Threads

XML ID ... 7
XMLElement and Type Parse. 1
Excluding / Removing empty elements from XElement 1
XML Document. How can I do this? 1
b-tree 11
How to shift a byte-array? 2
copy byte array 2
Byte[] Conversion 15

Top