Results 1 to 5 of 5

Thread: Resolved-String to Byte[]?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    26

    Resolved-String to Byte[]?

    OK, there's two ways to do this, but I only need the 2nd one.
    1) Convert string to corresponding byte[] value (i.e "adslfI" == {0x44, 0x55)) (Not really true, but you get the idea) I don't need this one!
    and
    2) Convert it as if it was a byte array, so: in a textBox I put in: "44 AF 8E", and then it's converted to a byte array: {0x44, 0xAF, 0x8E}.

    I've looked at various ASCII encoding and none get the job done, probably needs a for loop and some other crap...
    Last edited by gamesguru; Sep 4th, 2007 at 02:13 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String to Byte[]?

    Code:
    byte[] data = System.Text.Encoding.ASCII.GetBytes(myString);
    GetString would do the equivalent in the other direction.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    26

    Re: String to Byte[]?

    Does not work. I need this to convert it like so:
    If I put "44 AF" into a textBox then convert it, the byte array results with the value {0x44, 0xAF}, not some completely irrelevant value...and anything else besides 0 1 2 3 4 5 6 7 8 9 A B C D E F will cause an unhandled exception. This is not the same as converting an integer value 0x44AF to byte array!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String to Byte[]?

    Ah, it seems I didn't read your post carefully enough. The values returned by GetBytes are not irrelevant. Each byte represents the ASCII value of the corresponding character in the string. Hardly irrelevant but not what you want. What you want is this:
    Code:
    private byte[] GetBytes(string str)
    {
    	string[] substrings = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    	int length = substrings.Length;
    	byte[] bytes = new byte[length];
    
    	for (int index = 0; index < length; index++)
    	{
    		bytes[index] = Convert.ToByte(substrings[index], 16);
    	}
    
    	return bytes;
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    26

    Re: String to Byte[]?

    That's exactly what I wanted, thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width