0

I want define a byte array like "\x90<>",

I can only define it use numbers

byte[] a = new byte[] { 0x90, 0x3c, 0x3e };

but it's not readable and cost more time to write,

Can I do something like this in .net?

byte[] a = '\x90<>';


Edited.

I'm ask this because in c and c++ I can actually define a byte array by

char myvar[] = "\x90<>"; or char *myvar= "\x90<>";

they are equals new byte[] { 0x90, 0x3c, 0x3e} on c#.

6
  • characters in C# are not bytes Commented Dec 10, 2013 at 4:51
  • yes, range of char is 0-65535 and byte is 0-255, I just want the style like this. Commented Dec 10, 2013 at 4:59
  • You can create a custom converter to convert from your format to hex format Commented Dec 10, 2013 at 4:59
  • I'm a little lost as to why \x is equal to 0x90? Commented Dec 10, 2013 at 5:02
  • You can read en.wikipedia.org/wiki/String_literal Commented Dec 10, 2013 at 5:09

3 Answers 3

1

A string is an array of chars, where a char is not a byte in the .Net.

You can't define bytes with a string and you can't do this in easy way, you must implement your own convertor methods and use it with that.

Check this out:

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

static string GetString(byte[] bytes)
{
    char[] chars = new char[bytes.Length / sizeof(char)];
    System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
    return new string(chars);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try converting it to a char array in the declaration. The escaped characters will be correctly processed into a single char and the unescaped characters will be split into individual characters.

Then you can cast each element in the array as a byte. You could use LINQ to make it easy:

var bytes = "\x90<>".ToCharArray().Select(b => (byte)b);
foreach(var myByte in bytes){
    Console.WriteLine(string.Format("0x{0:x2}", myByte));
}

I believe this gives you the correct output of 0x90, 0x3c, 0x3e

Edit

I'm sure there will be some issue with encoding here, but none was specified in the question.

Extra Edit

The code above will give you an IEnumerable<byte>. To get the actual byte[] you want, just call

bytes.ToArray();

Comments

0

Convert to byte array as this.

 string source = "\x90<>";
 byte[] bytes = Encoding.Default.GetBytes(source);

3 Comments

It's not correct, result is 0x63, 0x60, 0x62, and I except 0x90, 0x3c, 0x3e.
Did you checked the result of new byte[] { 0x90, 0x3c, 0x3e };?
-new byte[] { 0x90, 0x3c, 0x3e } {byte[3]} byte[] [0] 144 byte [1] 60 byte [2] 62 byte -Encoding.Default.GetBytes("\x90<>") {byte[3]} byte[] [0] 144 byte [1] 60 byte [2] 62 byte

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.