string values to byte array without converting
Asked Answered
I

5

5

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.

Inotropic answered 6/6, 2011 at 15:1 Comment(16)
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?Groats
so you want to turn a String into a Byte[] and then back? Is this homework?Megasporangium
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.Sepaloid
@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 stringInotropic
@Gisli: But what was the original data? Text or binary data? It's really unclear where this data has come from or what you're trying to do. Why do you need it to be a byte array when the conversion you've shown just gets you back to a string?Groats
@AllenG. No this is not homework, thanks for asking. The problem is I have the the byte[], only it is in a string format, so I need to get the contents of the string into a byte array without encoding the data. I´m sorry I can´t be clearer.Inotropic
@Jon Skeet. The data is encoded and I need to decode it. The input is an encoded string. Sorry, I´m really bad at explaining what I mean.Inotropic
@Gisli: "an encoded string" doesn't explain how it's encoded. Base64? Hex? Was it originally text data or binary? All of this is very important.Groats
@Gisli: One of explaining is by posting a little more code. We all speak C#. And mention Db classes by name.Villiers
@Jon Skeet. I don't have the source code for how it is encoded. My coworkers are pretty sure that it is encoded by using: System.Text.Encoding.UTF8.GetBytes(string). But the man who did this is not here anymore and I haven´t been able to locate the source code.Inotropic
@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...Groats
@Jon Skeet. The name of the function is misleading. At first I was told I was decoding a base64 string and that´s why it called that. I´ve added the function to the original post.Inotropic
@Gisil: Wow, that's possibly the worst method name I've ever seen (and the implementation is pretty awful too). If this is meant to be encoding binary data (e.g. images) then basically you're already losing data.Groats
@Jon Skeet. I know about the name, I´m just trying to get this to work in the first place. I´ve read the thread where you suggest using Convert.ToBase64String but that won't work in my case. Could you point me in the right directionInotropic
@Gisli: Not really, without more information. If you can't change what's writing to the database, and it's writing in a way which loses information, you're screwed. You still haven't explained whether the data it's trying to represent is genuinely binary data.Groats
@Jon Skeet. As you see I found out that the data was in HEX and the conversion turned out to be quite easy. I´ve edited my original post and posted my solution. Thank you for all your help.Inotropic
S
9

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.

Sylviasylviculture answered 6/6, 2011 at 15:12 Comment(4)
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().Inotropic
@Gisli: Add a reference to System.Core and a using System.Linq; directive at the top of your code.Sylviasylviculture
Thanks. Now I get an error message: Could not find any recognizable digits. I´ll let you know if I figure it out.Inotropic
I need to store some data in my app.config file that i dont want to be in plain text, same concept. this helped thanks. -1 to whoever down voted the OP, i hate SO snobs who think that everyone on here is a professional developer. we all have to start somewhere. Great answerBrame
V
1

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.

Villiers answered 6/6, 2011 at 15:5 Comment(0)
C
1

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 single byte in the array.

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

var MyEncoding = Encoding.GetEncoding("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.

Compulsive answered 6/6, 2011 at 15:7 Comment(0)
N
1

If anyone still needs it this worked for me:

byte[] result = Convert.FromBase64String(str);
Nucleolar answered 17/1, 2022 at 12:38 Comment(0)
P
0

Have you tried:

string s = "....";
System.Text.UTF8Encoding.UTF8.GetBytes(s);
Postfree answered 6/6, 2011 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.