Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to copy a string into a byte[]

How to copy a string into a byte[]

Scheduled Pinned Locked Moved C#
csharpc++data-structuresbeta-testingtutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GDavy
    wrote on last edited by
    #1

    Hello, I want to copy a string into a byte[] array. string has a method ToCharArray that returns a char[] but that`s just not good enough, since a char is 2 bytes. Is there a quick and easy way to accomplish this? In unmanaged C++ I could just do a memcpy or memmove into a designated BYTE array, is there something similar I can use in C#? Thanks for your feedback, Davy

    L S L 3 Replies Last reply
    0
    • G GDavy

      Hello, I want to copy a string into a byte[] array. string has a method ToCharArray that returns a char[] but that`s just not good enough, since a char is 2 bytes. Is there a quick and easy way to accomplish this? In unmanaged C++ I could just do a memcpy or memmove into a designated BYTE array, is there something similar I can use in C#? Thanks for your feedback, Davy

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      byte[] mybyte = Encoding.ASCII.GetBytes("Hello World");

      1 Reply Last reply
      0
      • G GDavy

        Hello, I want to copy a string into a byte[] array. string has a method ToCharArray that returns a char[] but that`s just not good enough, since a char is 2 bytes. Is there a quick and easy way to accomplish this? In unmanaged C++ I could just do a memcpy or memmove into a designated BYTE array, is there something similar I can use in C#? Thanks for your feedback, Davy

        S Offline
        S Offline
        szukuro
        wrote on last edited by
        #3

        byte[] bytes = System.Text.Encoding.ASCII.GetBytes("MyString"); where ASCII can be replaced with other available encodings.

        1 Reply Last reply
        0
        • G GDavy

          Hello, I want to copy a string into a byte[] array. string has a method ToCharArray that returns a char[] but that`s just not good enough, since a char is 2 bytes. Is there a quick and easy way to accomplish this? In unmanaged C++ I could just do a memcpy or memmove into a designated BYTE array, is there something similar I can use in C#? Thanks for your feedback, Davy

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          System.Text.Encoding. A byte sequence means nothing without the information on how to map the byte sequence to characters. This is what the Encoding classes do.

          G 1 Reply Last reply
          0
          • L lmoelleb

            System.Text.Encoding. A byte sequence means nothing without the information on how to map the byte sequence to characters. This is what the Encoding classes do.

            G Offline
            G Offline
            GDavy
            wrote on last edited by
            #5

            Thanks all That`s what I was looking for! :rose:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups