Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to put the values of a string into a byte array with out changing the characters. This is because the string is in fact a byte representation of the data.

The goal is to move the input string into a byte array and then convert the byte array using:

string result = System.Text.Encoding.UTF8.GetString(data);

I hope someone can help me although I know it´s not a very good description.

EDIT:

And maybe I should explain that what I´m working on is a simple windows form with a textbox where users can copy the encoded data into it and then click preview to see the decoded data.

EDIT:

A little more code: (inputText is a textbox)

    private void button1_Click(object sender, EventArgs e)
    {
        string inputString = this.inputText.Text;
        byte[] input = new byte[inputString.Length];
        for (int i = 0; i < inputString.Length; i++)
        {
            input[i] = inputString[i];
        }
        string output = base64Decode(input);
        this.inputText.Text = "";
        this.inputText.Text = output;
    }

This is a part of a windows form and it includes a rich text box. This code doesn´t work because it won´t let me convert type char to byte. But if I change the line to :

    private void button1_Click(object sender, EventArgs e)
    {
        string inputString = this.inputText.Text;
        byte[] input = new byte[inputString.Length];
        for (int i = 0; i < inputString.Length; i++)
        {
            input[i] = (byte)inputString[i];
        }
        string output = base64Decode(input);
        this.inputText.Text = "";
        this.inputText.Text = output;
    }

It encodes the value and I don´t want that. I hope this explains a little bit better what I´m trying to do.

EDIT: The base64Decode function:

    public string base64Decode(byte[] data)
    {
        try
        {
            string result = System.Text.Encoding.UTF8.GetString(data);
            return result;
        }
        catch (Exception e)
        {
            throw new Exception("Error in base64Decode" + e.Message);
        }
    }

The string is not encoded using base64 just to be clear. This is just bad naming on my behalf.

Note this is just one line of input.

I've got it. The problem was I was always trying to decode the wrong format. I feel very stupid because when I posted the example input I saw this had to be hex and it was so from then on it was easy. I used this site for reference: http://msdn.microsoft.com/en-us/library/bb311038.aspx

My code: public string[] getHexValues(string s) { int j = 0; string[] hex = new String[s.Length/2]; for (int i = 0; i < s.Length-2; i += 2) { string temp = s.Substring(i, 2); this.inputText.Text = temp; if (temp.Equals("0x")) ; else { hex[j] = temp; j++; }

        }

        return hex;
    }

    public string convertFromHex(string[] hex)
    {
        string result = null;
        for (int i = 0; i < hex.Length; i++)
        {
            int value = Convert.ToInt32(hex[i], 16);
            result += Char.ConvertFromUtf32(value);
        }
        return result;
    }

I feel quite dumb right now but thanks to everyone who helped, especially @Jon Skeet.

share|improve this question
3  
What do you mean by "the string is in fact a byte representation of the data"? A string is a sequence of characters. How was your original binary data converted into a string? –  Jon Skeet Jun 6 '11 at 15:02
    
so you want to turn a String into a Byte[] and then back? Is this homework? –  AllenG Jun 6 '11 at 15:04
    
BTW, BitConverter has GetBytes method which can convert char into byte[]. You can use that if you don't like encoding due to some reason. Also it doesn't require passing Encoding since all .Net string are UTF strings anyway. –  Snowbear Jun 6 '11 at 15:10
    
@Jon Skeet. The string is stored in a database. When the string is sent in it is converted to a byte array, but the database saves it as a string or varchar. So when I fetch the data it is a string, but I need it to be a byte array so that I can use the conversion shown above. You can think of it as values of a byte array in a string –  Gisli Jun 6 '11 at 15:12
1  
@Gisli: So what is the original data? If it's not text data, then it should absolutely not use something like Encoding.UTF8. Your sample code uses Base64 - is it actually base64 data? If you could post some sample text, that might help... –  Jon Skeet Jun 6 '11 at 16:47

4 Answers 4

up vote 1 down vote accepted

Are you saying you have something like this:

string s = "48656c6c6f2c20776f726c6421";

and you want these values as a byte array? Then:

public IEnumerable<byte> GetBytesFromByteString(string s) {
    for (int index = 0; index < s.Length; index += 2) {
        yield return Convert.ToByte(s.Substring(index, 2), 16);
    }
}

Usage:

string s = "48656c6c6f2c20776f726c6421";
var bytes = GetBytesFromByteString(s).ToArray();

Note that the output of

Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(bytes));

is

Hello, world!

You obviously need to make the above method a lot safer.

share|improve this answer
    
thanks for answering. This is what I mean but when I try to use the ToArray() functions I get an error saying there is no definition for ToArray(). –  Gisli Jun 6 '11 at 16:27
    
@Gisli: Add a reference to System.Core and a using System.Linq; directive at the top of your code. –  Jason Jun 6 '11 at 19:03
    
Thanks. Now I get an error message: Could not find any recognizable digits. I´ll let you know if I figure it out. –  Gisli Jun 7 '11 at 8:31

Encoding has the reverse method:

byte[] data  = System.Text.Encoding.UTF8.GetBytes(originalString);

string result = System.Text.Encoding.UTF8.GetString(data);

Debug.Assert(result == originalString);

But what you mean 'without converting' is unclear.

share|improve this answer

Have you tried:

string s = "....";
System.Text.UTF8Encoding.UTF8.GetBytes(s);
share|improve this answer

One way to do it would be to write:

string s = new string(bytes.Select(x => (char)c).ToArray());

That will give you a string that has one character for every byte in the array.

Another way is to use an 8-bit character encoding. For example:

var MyEncoding = Encoding.GetEncoder("windows-1252");
string s = MyEncoding.GetString(bytes);

I'm think that Windows-1252 defines all 256 characters, although I'm not certain. If it doesn't, you're going to end up with converted characters. You should be able to find an 8-bit encoding that will do this without any conversion. But you're probably better off using the byte-to-character loop above.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.