-1

I have the following code:

string s =   "2563MNBJP89256666666685755854";
            Byte[] bytes = encoding.GetBytes(s);
            string hex = "";
            foreach (byte b in bytes)
            {
                int c=b;
                hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(c.ToString()));
            }

It prints the hex values . How can I add it in a vector ob bytes like this?

new byte [0x32,0x35..]

In hex I have : 323536....and so on. The next step is to add then in a byte[] vector in the following format 0x32,0x35..and so on; How to do this?

THX

daniel
  • 29
  • 5

4 Answers4

1

Isn't bytes already the list of bytes you want?

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
  • how to chech that? how to print the bytes values? – daniel May 18 '11 at 08:47
  • No it's not the same. The values are different:). – daniel May 18 '11 at 08:48
  • in s the results are: 50,53,54.. and in hex: 32,35,36 and so on – daniel May 18 '11 at 08:49
  • in s the results are in char! and in hex the results are in hexa values. – daniel May 18 '11 at 08:52
  • @daniel: Those are the same numbers. The first list is just represented in decimal while the second one is in hexadecimal. – Matti Virkkunen May 18 '11 at 08:54
  • I want the results in hexadecimal. How to convert them? – daniel May 18 '11 at 08:56
  • @daniel: Isn't that what your loop is doing? Converting them? I think you're confused as to what a number is. And judging by the comments on your first post you, it's not going to be worth my time to explain to you what a number is. – Matti Virkkunen May 18 '11 at 08:57
  • I am doing an encryption:). The result is different if i send encoding.getBytes(string) or if i send new byte[]{0x32,0x35..}. Where string is the string I want to have in hexadecimal – daniel May 18 '11 at 09:01
  • @daniel: I trust you will not be asking this question again, now that you have an accepted answer for it? Re-asking the same question isn't acceptable behavior here. If you want to draw attention to your question, you will be allowed to [place a bounty on it](http://stackoverflow.com/faq#bounty) after two days. You can also [edit your question](http://stackoverflow.com/posts/6042016/edit) to add additional information, which may make your question easier to understand and answer. –  May 18 '11 at 13:14
0

C#: System.Text.Encoding.ASCII.GetBytes("test")

Botz3000
  • 39,020
  • 8
  • 103
  • 127
0

For C# you could try

System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(strVar);//strVar is the string variable
FIre Panda
  • 6,537
  • 2
  • 25
  • 38
  • is there a link for an converter in ASCII, string, bytes and others? I knoe there was one..but i just can;t find the link – daniel May 18 '11 at 07:49
0

in C# you can use Encoding.GetBytes Method

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184