String to Byte Array

This is a discussion on String to Byte Array within the C# Programming forums, part of the General Programming Boards category; Hello, I've got the below code. What I'm trying to do is get the values entered by the user into ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    52

    String to Byte Array

    Hello,

    I've got the below code. What I'm trying to do is get the values entered by the user into textbox4 and place them into a byte array.

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                Byte[] OUTBuffer = new byte[65];
                int decValue = Convert.ToInt16(textBox4.Text);
                OUTBuffer[0] = 0;				
                OUTBuffer[1] = 0x80;
                byte[] intBytes = BitConverter.GetBytes(decValue);
                Array.Copy(intBytes, 0, OUTBuffer, 2, intBytes.Length);
                Console.WriteLine(OUTBuffer[2] + OUTBuffer[3]);    
    
            }
    But it's not working right. Once I go over the value of 255 into the text box I do not get the correct value displayed in the console when i try adding OUTBuffer[2] + OUTBuffer[3].

    Any help here will be appreciated.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    What exactly do you get and what do you think is the correct value?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    If this is an ASCII string...Encoding.ASCII.GetBytes(string).

    http://msdn.microsoft.com/en-us/library/ds4kkd55.aspx

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    52
    Quote Originally Posted by nvoigt View Post
    What exactly do you get and what do you think is the correct value?
    For example I entered 800 into the text box and the result in the console is 35. I should be getting the same integer value returned.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    First an Int32 is 4 bytes long. You are only looking at two of the bytes and saying it should be the same. Now this isn't your issue, but ...

    Did you really expect the values to add up to 800? Do you not understand that the first byte is how many '1's are in the number, and that the 2nd byte is how many '256's are in the number? If you actually look at the values stored you'll see that the first is 32 and the second is 3. 3 * 256 = 768, 768 + 32 = 800 so you are getting exactly the right value.

    Review number bases and how positional number systems (like our decimal system or this one, which is technically base 256) work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to change HEX String to Byte Array
    By homoon in forum C Programming
    Replies: 5
    Last Post: 08-12-2012, 11:20 PM
  2. converting string to byte in c++
    By Klutz15 in forum C++ Programming
    Replies: 9
    Last Post: 06-14-2011, 07:54 AM
  3. Hex String to byte array conversion
    By IfYouSaySo in forum C# Programming
    Replies: 3
    Last Post: 06-15-2006, 04:59 PM
  4. ANSI C ASCII String to HEX byte array conversion
    By phyte in forum C Programming
    Replies: 10
    Last Post: 12-14-2004, 07:02 AM
  5. Adding a byte to a string
    By psul in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2004, 02:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21