Base64 and Utf8. What am I missing?

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following:
String result = Convert.ToBase64String(Encoding.UTF8.GetBytes
("Test"));

Shouldn't result become "Test"? I get "VGVzdA=="

I am using this approach to save Byte data into a XML file and get it
back to a string.
It isn't working so I made this test and I was expecting to get "Test"
in result.

Thanks,
Miguel
 
Arne said:
Looks correct.


Base64 is for binary data.

If you have text data, then you really don't need base64.

Arne

Well, in this case he has binary data that happens to look good when
written as text. :)
 

I am a little bit confused.
Ok, so I convert a String to Byte and then to Base64.
How can I do the inverse convertion: Base64, to Byte[] and then to
String?

Thanks,
Miguel
 
shapper said:
shapper said:
shapper wrote:
Family Tree Mike wrote:
Arne Vajhøj wrote:
shapper wrote:
I have the following:
String result = Convert.ToBase64String(Encoding.UTF8.GetBytes
("Test"));
Shouldn't result become "Test"?
No.
I get "VGVzdA=="
Looks correct.
I am using this approach to save Byte data into a XML file and get it
back to a string.
It isn't working so I made this test and I was expecting to get "Test"
in result.
Base64 is for binary data.
If you have text data, then you really don't need base64.
Well, in this case he has binary data that happens to look good when
written as text. :)
:-)
I am a little bit confused.
Ok, so I convert a String to Byte and then to Base64.
How can I do the inverse convertion: Base64, to Byte[] and then to
String?
ConvertFromBase64 will do String->byte[]
Encoding.UTF8.GetString will do byte[]->String
But again: why??
I was doing that but somehow I mixed everything.
Basically I am working on a system to store assets on a SQL server or
XML files.
The assets can be Files or Strings.
I am making the conversions and saving the content as follows:
In a XML File:
Presentation Domain Saved As
String > Byte[] > Base64String (Text or html code)
Byte[] > Byte[] > Base64String (File)
In a SQL Server:
Presentation Domain Saved As
String > Byte[] > Byte[] (Text or html code)
Byte[] > Byte[] > Byte[] (File)
Does this make sense?
I would use:

In a XML File:

Presentation Domain Saved As
String > String String
not > Byte[] > Base64String

In a SQL Server:

Presentation Domain Saved As
String > String > VARCHAR
not > Byte[] > VARBINARY

In relation to Presentation layer (In this case are the controllers of
a MVC application) I have the file in HttpFilePostedBase (Not Byte. I
wrote it wrong).

In SQL I am saving the files as VarBinary:
[File] varbinary(max) filestream constraint DF_Documents_File default
(0x)

In relation to string I was trying to avoid having two colums on the
database and save in one or in another.
I was looking to have only one column for the content and another one
for the Mime Type of the content. Is it wrong?

In relation to XML I was saving both in Base64String just to use the
some conversion code and not have so many decisions on the way.

I am a bit skeptical about an approach that tried to treat
String and File identical. It will break as soon as you want
to store more meta data about the file.

Item
id
typ

StringItem
id
txt

FileItem
is
name
body
x
y
z

will probably be better in the long run.

Arne
 
Back
Top