0

I have a string in c# which contains the following..

SMDR#0D#0APCCSMDR#0D#0A

Now as per my requirement i need to parse this into byte[] with the same formate..I have to show the byte[] as

SMDR#0D#0APCCSMDR#0D#0A

This i am needed because the my function takes arguments in this formate of byte[] and the value that i need to pass is this only..

SMDR#0D#0APCCSMDR#0D#0A

How to achieve this.Do i need to change in the string formate or byte[]..

1
  • Can you make an effort and put a bit of context in your question? How does this function need to manipulate the byte[]? What text enconding does it need to understand? Commented Jan 14, 2014 at 6:09

4 Answers 4

4

You can do it using following code, I hope this will answer your question.

FROM http://www.dotnetperls.com/convert-string-byte-array

using System;
using System.Text;

class Program
{
    static void Main()
    {
  // Input string.
  const string input = "Dot Net Perls";

  // Invoke GetBytes method.
  // ... You can store this array as a field!
  byte[] array = Encoding.ASCII.GetBytes(input);

  // Loop through contents of the array.
  foreach (byte element in array)
  {
      Console.WriteLine("{0} = {1}", element, (char)element);
  }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You are making a lot of assumptions
What assumptions? I would write the same thing, rather than a string=>linq=>array that silently discards data. The assumption is now right there... Encoding.Ascii. Change that if you need.
3

I'm really confused with your question, but if you need your string represented as byte[] you can use LINQ:

var output = input.Select(x => (byte)x).ToArray();

1 Comment

This will silently discard the data if the char is not in the ASCII range. If that is desired, then sure. But why not just use Encoding.ASCII or Encoding.UTF8.GetBytes()
2

Well, here is hopefully a more explanatory answer...

Getting the bytes doesn't technically depend on the encoding, since it is just memory. However, interpreting the sequence of bytes does. What encoding does the function you are calling expect?

If it is expecting raw ascii bytes:

Encoding.ASCII.GetBytes("hello, 你是天才"); // 11 bytes. chinese truncated

If it is expecting the UTF-8 data (1 byte ascii, 2-4 bytes for others):

Encoding.UTF8.GetBytes("hello, 你是天才"); // 19 bytes, as utf8 

If it wants a double width characters (2 bytes each, UCS-2):

Encoding.Unicode.GetBytes("hello, 你是天才") // 22 bytes

And so on and on. Check out the System.Text.Encoding class for more details and options.

Comments

0

just try this code

string str="SMDR#0D#0APCCSMDR#0D#0A";
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
 foreach (var item in bytes)
        {
            Response.Write((char)item);
        }

for more clarify read from http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy(v=vs.110).aspx

2 Comments

How to take the output of this line System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); into byte[] array
@Adi BlockCopy copy all char of a string parameter, to byte array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.