binary data to and from text conversion - c#

Sometimes i generate data that is as little as 1k. I thought it may be good if i could convert it to text so i can paste it to someone on a forum or on MSN. How can i convert data into text then convert back? How many bits can i use? I must have it compatible with pasting on forums and i would like it to be compatible with msn if the string isnt to long. How can i make the data text safe?
Should i assume the data is only allowed to be 6bits? Is 32-127 the only values available? will i have a problem copy/pasting 127 (ascii for 'DEL'). I am using C#

For safe copy and paste, consider UUEncoding or Base64Encoding. .NET has support for Base64 on the Convert class.
Finding sample code in C# that implements UUEncoding or Base64Encoding isn't too hard:
<Link>
http://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/

I think the simplest thing would be to use base64 encoding. It will take any binary data and convert it to text suitable for pasting anywhere.
In C# you can use Convert.ToBase64String (or something like this)

Does your forum allow file attachments? If so just attach the binary file.

Related

DynamoDB automatically converting special characters

Working with DynamoDB and AWS (.net C#). For some reason when saving strings containing "é" get replaced with a question mark when saved.
How can I prevent it from happening?
DynamoDB stores strings in UTF-8 encoding. Somewhere in the your application you must be assigning that string in something other than UTF-8.
Im using Java (which uses UTF-16). I don't do anything special when storing strings. I just tried storing and retrieving "é" in DynamoDB using the Java SDK and there was no problem.
Just to add to the previous answer, your IDE will generally have encoding settings. You'll want to change the string encoding for your project to UTF-8 to minimize the changes of an encoding error, which can create what is thought to be an unknown string.
For example, in Eclipse editors, you can see this answer to change your encoding.

C# My own file format

I'm looking to make my own file format .
that format should contains pictures/pdf/and other files ...
also need to know how I can packer-unpacker for this format to unpack files from it/pack in it & reading the pictures from my own format to picture boxes on my WinForm for example.
I've searched but didn't really found what I'am looking for
I hope someone can help me , thank you
Zip is an excellent choice. Because you can encrypt the file and of course reduce the file size in some cases (text and uncompressed things). But if you want to create your own file format you can easily decide rules for your storage and order inside the file. Then serialize the info into the file. For example by object serialization or by writing the binary date to file object by object .
if you really want to write your own file format then I would suggest one of two things. One, you could do it entirely in binary at which point you would want to do a 'chunk' format. Chunk format is to basically have a header to each subsection. The header contains the size of both the header as well as the size of the payload. Create a serialization class for your header then add the bytes to the filestream from your payload. Actually pretty easy to do.
Second (and easier) way to do this would be to create an XML format. Create a master class for your format then add all of the data as collections of sub classes under that. Once you have that, use any of .net xml serialization classes to serialize it out to disk.
You can also use SQLite for your purposes. It provides dbms power without needing server. That is popular solution for your problem.
System.Data.SQLite is an ADO.NET adapter for SQLite.

Binary Image Extension?

When we convert an image to binary data, (let's say a .png image) is there a way to get the extension back while converting the binary to image again in .net?
Short answer, no. You can't get the name either. The file name is not generally stored in image data.
If you know what the image format is you can use either a sensible, generally recognised extension or a file extension registered to that file type on your system. Hopefully, these will not differ.
If you don't know the format perhaps you could read it before serialising to binary and prefix it to the representation.
For a less general answer please expand your question.
EDIT
I guess you could attempt to display the image using a set of potential formats, then visually assess all succesful decodes to choose the correct format. Somehow, it seems easier to just include the original extension in the binary serialization.

Reading a binary file as text and manipulating it... [C#] Versus [VB.NET]

Well guys I am in a bit of a pickle here...
I am doing some exercises on encrypting data. One of them are binary files. I am currently using triple DES to encrypt and decrypt the files both in VB.NET and C#...
Now the thing is, once it is decrypted in VB.NET and saved, i can execute it again...
But for some reason, my C# file is bigger! 20,4K where VB.NET one is 19,0. The C# file also is rendered unexecutable...
Upon a closer look. The files appear almost exactly the same, but C# seems to add in a few extra bytes here and there in (seemingly) random places...
I am currently using File.ReadAllText(String filepath, Encoding encoding); with UTF-8 encoding
thanks!
You say you're using File.ReadAllText... but also that these are binary files. That makes me suggest that you're treating opaque binary data (e.g. the result of encryption) as if it were text (e.g. calling Encoding.GetString on it).
Don't do that.
Basically, encryption generally works on binary data - binary in, binary out. If you need to encrypt text to text, you'll usually apply a "normal" encoding to convert the text to binary data (e.g. Encoding.UTF8.GetBytes(text)) and then use Base64 to convert the opaque binary data to text in a lossless way - e.g. with Convert.ToBase64String(encrypted).
Decrypting is just the reverse: use Convert.FromBase64String(encryptedText) to get the encrypted binary data, decrypt it, and then use Encoding.UTF8.GetString(decrypted) to get back to the text.

How to convert the encoding of an string to UTF-8 without know the original encoding in C#?

I'm reading a CSV file with Fast CSV Reader (on codeproject). When I print the content of the fields, the console show the character '?' in some words. How can fix it?
The short version is that you have to know the encoding of any text file you're going to read up front. You could use things like byte order marks and other heuristics if you really aren't going to know, but you should always allow for the value to be tweaked (in the same way that Excel does if you're importing CSV).
It's also worth double checking the values in the debugger, as it may be that it is the output that is wrong, as opposed to the reading -- bear in mind that all strings are Unicode internally, and conversion to '?' sounds like it is failing converting the unicode to the relevant code page for the console.

Categories

Resources