Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to GetBytes from string appropriately?

Tags:

c#

encoding

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;
}
like image 488
John Avatar asked Dec 01 '11 08:12

John


People also ask

How do you use string getBytes?

getBytes() The method getBytes() encodes a String into a byte array using the platform's default charset if no argument is passed. We can pass a specific Charset to be used in the encoding process, either as a String object or a String object.

Which of the following are the correct variant of getBytes method?

There are three variants of getBytes() method. The signature or syntax of string getBytes() method is given below: public byte[] getBytes() public byte[] getBytes(Charset charset)

How do you byte a string?

One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.

Can we convert string to byte?

We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument.


1 Answers

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.

like image 133
Marc Gravell Avatar answered Oct 13 '22 00:10

Marc Gravell