Results 1 to 12 of 12

Thread: [2.0] string to byte array?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    [2.0] string to byte array?

    haven't used C# in a long time and forgot how to convert a string to a bytearray. Anyone got a function to do it?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] string to byte array?

    (new System.Text.Encoding.UTF8Encoding()).GetBytes(mystring)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] string to byte array?

    Also is there a multiline function for strings?

    Like lua(scripting langauge) you could do

    Code:
    local multilinestring = [[this
    is
    a
    multi line string]]

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] string to byte array?

    no, but you can put \n in a string for a new line.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] string to byte array?

    Quote Originally Posted by penagate
    (new System.Text.Encoding.UTF8Encoding()).GetBytes(mystring)
    That didnt work. UTF8Encoding() doesnt exist.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] string to byte array?

    System.Text.Encoding.UTF8.GetBytes(mystring)

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] string to byte array?

    Now is there an easy way to use " in strings?

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] string to byte array?

    "\""

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] string to byte array?

    Quote Originally Posted by penagate
    "\""
    Are there special quotes like in lua you can do [["anything in here"]] because it would take too long to add all those \ before the quotes.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] string to byte array?

    well, C# isn't lua.

    Long string literals are a bad idea anyway, if you want long strings with quotes then put them in a resource file and load from there.

    Also for paths, regular expressions and other things that contain lots of backslash characters you can prepend @ to disable escape sequence parsing so you don't have to escape \ as \\.

    Code:
    string file_path = @"C:\blah\thing";

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2.0] string to byte array?

    Now also i got a number as a string and am wondering how do i convert it to a number?

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

    Re: [2.0] string to byte array?

    Quote Originally Posted by high6
    Now also i got a number as a string and am wondering how do i convert it to a number?
    Use the Parse or TryParse method of the type you want to convert the string to. If you know for sure that the string represents a valid number then use the Parse method. If there's a chance that it may not be valid, like if it's been entered by the user in a TextBox, then use the TryParse method.
    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

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