7

I have a string variable from which I get the following bytes with the following loop:

Bytes I get: 1e 05 55 3c *e2 *91 6f 03 *fe 1a 1d *f4 51 6a 5e 3a *ce *d1 04 *8c 

With that loop:

  byte[] temp = new byte[source.Length];
  string x = "";
  for (int i = 0;i != source.Length;i++)
  {
    temp[i] = ((byte) source[i]);
  }

Now I have wanted to simplify that operation and use Encoding's GetBytes. The problem is I cannot fit an appropriate encoding. e.g. I get several bytes incorrect:

Encoding.ASCII.GetBytes(source):    1e 05 55 3c *3f *3f 6f 03 *3f 1a 1d *3f 51 6a 5e 3a *3f *3f 04 *3f
Encoding.Default.GetBytes(source):  1e 05 55 3c  e2  3f 6f 03  3f 1a 1d  f4 51 6a 5e 3a  ce  4e 04  3f

How can I get rid of that loop and use Encoding's GetBytes?

Here is the summary:

Loop(correct bytes):                1e 05 55 3c *e2 *91 6f 03 *fe 1a 1d *f4 51 6a 5e 3a *ce *d1 04 *8c 

Encoding.ASCII.GetBytes(source):    1e 05 55 3c *3f *3f 6f 03 *3f 1a 1d *3f 51 6a 5e 3a *3f *3f 04 *3f
Encoding.Default.GetBytes(source):  1e 05 55 3c  e2  3f 6f 03  3f 1a 1d  f4 51 6a 5e 3a  ce  4e 04  3f

Thanks!

Addition:

I have a string input in hex, sth like: "B1807869C20CC1788018690341" then I transfer this into string with the method:

private static string hexToString(string sText)
{
  int i = 0;
  string plain = "";
  while (i < sText.Length)
  {
    plain += Convert.ToChar(Convert.ToInt32(sText.Substring(i, 2), 16));
    i += 2;
  }
  return plain;
}
John
  • 1,834
  • 5
  • 32
  • 60
  • 1
    How do you get the string `source` from the bytes? – Mehrdad Afshari Dec 01 '11 at 08:40
  • 2
    could you provide the string in question? – yas4891 Dec 01 '11 at 08:40
  • 1
    @John, seriously: What are you trying to achieve? – yas4891 Dec 01 '11 at 08:50
  • @yas4891 ok, I noticed that I am transfering hex string to string and then to byte array. I could transfer straightforwardly from hex string to byte[] and that works ok.. But just for curiosity. How to do it correctly: hexString -> to string -> to byte array? – John Dec 01 '11 at 08:57
  • @John there is no single "correct" hexString -> to string -> to byte array; to go to/from `string`, you **really** need to know which `Encoding` it is in; if it is just arbitrary binary, then **it isn't a string**, and shouldn't be treated as such. Unless you cheat with 28591 (as per my answer) – Marc Gravell Dec 01 '11 at 09:12

2 Answers2

4

Your hexToString is transferring byte values (via hex) directly to unicode code-points in the range 0-255. As it happens, that ties into code-page 28591, so if you use:

Encoding enc = Encoding.GetEncoding(28591);

and use that enc, you should get the right data; however, a more important point here is that binary data is not the same as text data, and you should not use a string to hold arbitrary binary.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

Presuming that you are trying to "decode" a string literal:
C# stores the strings as Unicode internally. So you might want to use a encoding that (correctly) supports Unicode

such as:

Encoding.UTF8.GetBytes(source)
Encoding.UnicodeEncoding.GetBytes(source)

Note the caution given for Encoding.Default in MSDN

yas4891
  • 4,774
  • 3
  • 34
  • 55