Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ByteArray to string conversion and back

I have a uint value that I need to represent as a ByteArray and the convert in a string. When I convert back the string to a byte array I found different values. I'm using standard ASCII converter so I don't understand why I'm getting different values. To be more clear this is what I'm doing:

byte[] bArray = BitConverter.GetBytes((uint)49694);
string test = System.Text.Encoding.ASCII.GetString(bArray);
byte[] result = Encoding.ASCII.GetBytes(test);

The bytearray result is different from the first one:

bArray ->

[0x00000000]: 0x1e
[0x00000001]: 0xc2
[0x00000002]: 0x00
[0x00000003]: 0x00

result ->

[0x00000000]: 0x1e
[0x00000001]: 0x3f
[0x00000002]: 0x00
[0x00000003]: 0x00

Notice that the byte 1 is different in the two arrays.

Thanks for your support.

Regards

like image 745
lordpurple Avatar asked Nov 12 '12 12:11

lordpurple


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

string test = System.Text.Encoding.ASCII.GetString(bArray);
byte[] result = Encoding.ASCII.GetBytes(test);

Because raw data is not ASCII. Encoding.GetString is only meaningful if the data you are decoding is text data in that encoding. Anything else: you corrupt it. If you want to store a byte[] as a string, then base-n is necessary - typically base-64 because a: it is conveniently available (Convert.{To|From}Base64String), and b: you can fit it into ASCII, so you rarely hit code-page / encoding issues. For example:

byte[] bArray = BitConverter.GetBytes((uint)49694);
string test = Convert.ToBase64String(bArray); // "HsIAAA=="
byte[] result = Convert.FromBase64String(test);
like image 111
Marc Gravell Avatar answered Sep 21 '22 06:09

Marc Gravell


Because c2 is not a valid ASCII char and it is replaced with '?'(3f)

Converting any byte array to string using SomeEncoding.GetString() is not a safe method as @activwerx suggested in comments. Instead use Convert.FromBase64String, Convert.ToBase64String

like image 40
L.B Avatar answered Sep 22 '22 06:09

L.B