4 Replies - 383 Views - Last Post: 25 April 2014 - 05:38 AM Rate Topic: -----

#1 ro12hit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-April 14

convert string to byte array in c# is changing the value

Posted 25 April 2014 - 12:55 AM

I am trying to store each char of a string(string a ="1100") into a
byteArray ( byte[] byteArray = new byte[4];). its not showing any error but its storing like below:

byteArray[0] = 49
byteArray[1] = 49
byteArray[2] = 48
byteArray[3] = 48

and what i want is

byteArray[0] = 1
byteArray[1] = 1
byteArray[2] = 0
byteArray[3] = 0

I don't know why but its replacing 1 with 49 and 0 with 48.
Can anyone tell me what am I doing wrong or how to do this?
my code is as below

                   byte[] byteArray = new byte[4];)/>
                   int binArrayAdd = 0;
                   string a ="1100";
                   foreach (char Character in a)
                   {
                   byteArray [binArrayAdd] = Convert.ToByte(Character);
                   binArrayAdd++;
                   }



Is This A Good Question/Topic? 0
  • +

Replies To: convert string to byte array in c# is changing the value

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: convert string to byte array in c# is changing the value

Posted 25 April 2014 - 01:59 AM

The reason you are getting this result is because:
In ASCII, the code 48 is equivalent to the character 0
In ASCII, the code 49 is equivalent to the character 1
You aren't doing anything wrong in code.
Was This Post Helpful? 1
  • +
  • -

#3 ro12hit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-April 14

Re: convert string to byte array in c# is changing the value

Posted 25 April 2014 - 02:53 AM

Thanks DimitriV for the reply..........

I didn't knew about this, thanks. But I don't want it to change to 48 or 49. Is there a way to do that. I mean instead of storing as 48 can it store as 0 and instead of 49 as 1. if possible can u show me how to do it.
Was This Post Helpful? 0
  • +
  • -

#4 ro12hit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-April 14

Re: convert string to byte array in c# is changing the value

Posted 25 April 2014 - 04:59 AM

I got the solution of this problem. I was trying to convert char into byte. That's why it was taking the ASCII value. I just tried converting the char into string and then convert it into byte and its working fine now.
Thanks for the reply DimitriV...........
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,940
  • Joined: 05-May 12

Re: convert string to byte array in c# is changing the value

Posted 25 April 2014 - 05:38 AM

... or you could have simply subtracted '0' or 48 from each value
... or you could have had an if statement which assigned a 0 if the character was '0' and 1 otherwise
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1