Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# convert string into its byte[] equivalent

At this point most people will be thinking "Ah ill post this..:"

byte[] dataB= System.Text.Encoding.ASCII.GetBytes(data);

However.. the problem I have is i need the exact value of the bytes with no encoding just the pure value for each byte. For example if the value of the string is (0xFF32) i want it to convert it too {255,50}. he reason for this is I have a file format I am attempting to read which stores int's as bytes saves them and then reads them when the program opens.

This is what I have so far:

...
dialog.InitialDirectory =
    Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) +
    "/Test";

dialog.Title="Open File";

if (dialog.ShowDialog(this) == DialogResult.OK)
{
    StreamReader reader = new StreamReader(dialog.FileName);
    string data = reader.ReadToEnd();
    reader.Close();
    byte[] fileC = System.Text.Encoding.ASCII.GetBytes(data);
    File_Read(dialog.FileName,fileC);
}
...

So when I try and read the file it converts the file convents of say 0xFF into 0x3F because 0xFF is greater then 127 and 0x3F is a ?.

Sorry if i seem a bit confusing :)

Thanks, Michael

like image 851
redorkulated Avatar asked Feb 12 '09 19:02

redorkulated


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 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?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

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.


2 Answers

The problem is with your approach to start with:

I need the exact value of the bytes with no encoding

...

For example if the value of the string is (0xFF32)

That's a bit like looking at an oil painting and saying, "I want the bytes for that picture, with no encoding." It doesn't make sense. Text isn't the same as binary data. Once you understand that, it's easy to get to the root of the problem. What you really want is the contents of a file as a byte array. That's easy, because files are binary data! You shouldn't be reading it as text in the first place if it isn't really text. Fortunately, .NET makes this really easy:

byte[] fileC = File.ReadAllBytes(dialog.FileName);
like image 77
Jon Skeet Avatar answered Oct 07 '22 16:10

Jon Skeet


However.. the problem I have is i need the exact value of the bytes with no encoding just the pure value for each byte.

Then use this:

byte[] dataB = System.Text.Encoding.Unicode.GetBytes(data);

It returns the bytes as stored internally by .NET strings.

But all this is codswallop: A string is always linked to a particular encoding and there's no way around it. The above will fail e.g. if the file contains invalid Unicode code sequences (which may happen) or through normalization. Since you obviously don't want a string, don't read one. Read the file as binary data instead.

like image 20
Konrad Rudolph Avatar answered Oct 07 '22 16:10

Konrad Rudolph