Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2009, 04:41 PM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default Base64Binary converter

At work today I had to convert a file into a base64binary format. I found this website which works well.

This got me to thinking that as I am learning (read very new) to C# it would be a good idea to try and build something similar as a general learning experience.

So I am going to do it initially as a console application which a user will be able to enter data (standard iso format) which it will then convert and output as base64binary.

I have started with this code:

Code:
            string input1;
            Console.WriteLine("please enter your name:");
            input1 = Console.ReadLine();
            Convert.ToBase64String(input1);
            Console.WriteLine("{0}", input1);
            Console.ReadKey();
This doesn't work however because:

Error 1 The best overloaded method match for 'System.Convert.ToBase64String(byte[])' has some invalid arguments C:\BegVCSharp\CodeSnippets\CodeSnippets\Program.cs 41 13 CodeSnippets

:Error 2 Argument '1': cannot convert from 'string' to 'byte[]' C:\BegVCSharp\CodeSnippets\CodeSnippets\Program.cs 41 36 CodeSnippets

After some reading up I understand the second error message I can't take a string format and implicitly convert it to a byte format which is what I have tried to do. However if I work on an explicit conversion is it possible to do this?

What is the best way for me to read numbers, letters and special characters and be able to convert them into base64binary? Am I going about this the right way and is a console application capable of acheiving this?

I want to work it out myself but a point in the right direction never hurt anyone ;-)

Cheers,
 
Old September 24th, 2009, 09:31 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, part of the problem is that a STRING in .NET is *not* an array of bytes, or even very equivalent to one.

It is, instead, an array of Unicode characters (well...deep under the covers that's what it is).

Now, it's true that for normal ASCII characters, the upper bytes of all those multi-byte unicode characters are going to be zero, and one could imagine condensing the string by ignoring those bytes. But it's not a good practice.

So the right answer is:
-- In a loop on each character in the string
-- Convert the character in the string to an int16 value, using Convert.toInt16()
-- Convert the resultant int16 value into a pair of byte values using masking and shifting
-- Put the byte values into an array of bytes, one after the other
-- End of loop
-- Then you can finally call Convert.toBase64String passing that array of byte values

You might wonder why MS didn't providee an overload in toBase64String that would, indeed, take a string and do all that internally for you. But of course the reason is that MS stands for MicroSloppy.
 
Old September 24th, 2009, 09:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

N.B.: I suppose one can make the argument that to know how to code the string as Base64 you would need to know whether to encode the 2-bytes in each character high-byte first or low-byte first.

Indeed, that's a decision you will have to make if you use my suggested algorithm. And it means that a string encoded one way would be unreadable by a decoder that expected the bytes in the other order.

And I guess, to be fair, it's hard to imagine any valid reason for encoding a *string* as Byte64.
 
Old September 25th, 2009, 03:10 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 194
Thanks: 5
Thanked 3 Times in 3 Posts
Default

I picked the string as up until now I have been using it to store both numerical and alphabetical data. this to me meant that because I wanted to convert it into base64 which I have been able to do on the website that I thought it would be capable of handling both the datatypes.

It is probably a simplistic way of looking at it but that was my reasoning why.

It also sounds like your solution uses (well does use) some techniques which I do not know about yet so I will have to do some more reading up first before I can fully appreciate your help but thanks, I will let you know how I am getting on.





Similar Threads
Thread Thread Starter Forum Replies Last Post
currency converter anujrathi ASP.NET 3.5 Professionals 1 November 24th, 2009 05:47 AM
Base Converter iceman90289 C++ Programming 1 July 9th, 2008 07:44 PM
Base64binary to TIF aveekb Pro VB 6 0 February 13th, 2007 07:23 AM
converter anukagni ASP.NET 1.0 and 1.1 Basics 1 August 28th, 2006 07:43 AM
convert bytes[] into base64binary in asp.net bharat_agarwal XSLT 1 June 20th, 2006 02:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.