473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

check if string contains base64 strings

hey all
currently i use the following piece of code to check if the string passed to
me can be converted to base64, it is not very efficient and bad, can someone
please suggest another of doing this
private string ConvertBase64ToString(string[] content)
{
StringBuilder sb = new StringBuilder();
int i = content.GetUpperBound(0);
for (int j=0; j<i; j++)
{
try
{
// if successful in converting base64 append it
sb.Append(ASCIIEncoding.ASCII.GetString(Convert.Fr omBase64String(content.GetValue(j).ToString())));
}
catch (Exception)
{
// cannot be decoded so append the original string
sb.Append(content.GetValue(j).ToString());
}
}
return sb.ToString();
}
content passed to be looks like:
----
some string not containing base64
another strign without base64
base64base64base64stuffhere
morebase64base64stuffhere
some more strings without base64
----

is it possible to somehow find out if a line is base64 without attempting to
covert it and catching the exception?

thanks

Nov 22 '05 #1
1 7394
You can check the string only contains the Base64 alphabet (below) and zero or more padding chars ("=") at the *end of the string. You still need to try and catch as even a string that meets this test could fail Convert.FromBase64 (i.e. wrong padding, etc), but this simple test can catch the major cases. hth

Value Encoding Value Encoding Value Encoding Value Encoding
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y
--
William Stacey, MVP
http://mvp.support.microsoft.com

"mvdevnull" <mv*******@discussions.microsoft.com> wrote in message news:ED**********************************@microsof t.com...
hey all
currently i use the following piece of code to check if the string passed to
me can be converted to base64, it is not very efficient and bad, can someone
please suggest another of doing this


private string ConvertBase64ToString(string[] content)
{
StringBuilder sb = new StringBuilder();
int i = content.GetUpperBound(0);
for (int j=0; j<i; j++)
{
try
{
// if successful in converting base64 append it.
sb.Append(ASCIIEncoding.ASCII.GetString(Convert.Fr omBase64String(content.GetValue(j).ToString())));
}
catch (Exception)
{
// cannot be decoded so append the original string
sb.Append(content.GetValue(j).ToString());
}
}
return sb.ToString();
}


content passed to be looks like:
----
some string not containing base64
another strign without base64
base64base64base64stuffhere
morebase64base64stuffhere
some more strings without base64
----

is it possible to somehow find out if a line is base64 without attempting to
covert it and catching the exception?

thanks

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Magnus | last post by:
Hi, I'm programatically posting an image using multipart/form-data. It sends to the server OK, but when I try to view it in the browser, it is still in the base64 string I sent it as:...
1
by: mvdevnull | last post by:
hey all currently i use the following piece of code to check if the string passed to me can be converted to base64, it is not very efficient and bad, can someone please suggest another of doing...
7
by: Julia | last post by:
Hi I am trying to pass an encoded string to a JavaScript the following is the C# code which convert the string STRING_TO_ENCODE to base64 byte bytIn =...
5
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
7
by: Neo Geshel | last post by:
Greetings. I have managed to stitch together an awesome method of posting text along with an image to a database, in a way that allows an unlimited number of previews to ensure that text and...
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
11
by: cooltoriz | last post by:
Hello there, I just found that the compiled code won't hide the string variables so that I can see them by opening the execuable using Notepad. I have couple applications that have password...
9
by: Jeremy Kitchen | last post by:
Are there any library functions that can help me to do this? If necessary I can convert the string to a byte array. I don't want to have to write my own Hex conversion if it isn't necessary. ...
2
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to...
6
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.