C# convert string into its byte[] equivalent

52,241

Solution 1

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);

Solution 2

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.

Solution 3

//convert a string to a byte array

public static byte[] StrToByteArray(string str)
{
    System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
    return encoding.GetBytes(str);
}

//convert a byte array to a string

public string ByteArrayToStr(byte [] dBytes)
{
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
return enc.GetString(dBytes);
}

Solution 4

Use a BinaryReader.

Solution 5

Why convert from string at all? Couldn't you just read the contents of the file directly into bytes?

byte[] fileC = File.ReadAllBytes(dialog.FileName);
Share:
52,241
redorkulated
Author by

redorkulated

Updated on July 09, 2022

Comments

  • redorkulated
    redorkulated over 1 year

    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

  • redorkulated
    redorkulated over 14 years
    hey, im really new to C# just found an example to read files and thats what I cobbled together :P. Didnt know that files could be read anyother way :)
  • redorkulated
    redorkulated over 14 years
    i understand how files bit bytes and all that shizzle work which is why im able to understand storing (for example) a 64-bit integer as 8 bytes i just didnt know how to get at the info :); thansk for your help
  • Jon Skeet
    Jon Skeet over 14 years
    But treating a file as if it contains text when it doesn't is a fundamental misunderstanding which burns so many people. Any time you start thinking about "the contents of a string as bytes without an encoding" that almost certainly means there's a bug lurking.
  • redorkulated
    redorkulated over 14 years
    i wasnt looking at the "string" as text in this instance i was trying to just get the pure binary as thats what the file is storing. I do understand your point though so thanks :)
  • Jon Skeet
    Jon Skeet over 14 years
    Sorry to nag - I know it sounds patronising. Thanks for being understanding :)