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. String data into byte array without hex conversion

String data into byte array without hex conversion

Scheduled Pinned Locked Moved C#
questiondata-structures
6 Posts 3 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.
  • W Offline
    W Offline
    worked on mine
    wrote on last edited by
    #1

    How do I get string data into a byte array, programmatically, without doing a hex conversion. i.e., byte[i] = (???)string.Substring(0,2); I need the programmatic equivelent of: byte[] bytes = {0x14, 0x55, 0x90, 0x00, 0x01}; or byte[i] = {0x + string} I can't make any of this work. Need Guru! TIA, Steve

    D J 2 Replies Last reply
    0
    • W worked on mine

      How do I get string data into a byte array, programmatically, without doing a hex conversion. i.e., byte[i] = (???)string.Substring(0,2); I need the programmatic equivelent of: byte[] bytes = {0x14, 0x55, 0x90, 0x00, 0x01}; or byte[i] = {0x + string} I can't make any of this work. Need Guru! TIA, Steve

      D Offline
      D Offline
      DannyAdler
      wrote on last edited by
      #2

      Hi there, Maybe this will put you in the right track: string StringToConvert = "abc"; //This call will return a byte array with the numeric values of the chars byte[] bytes = StringToByteArray(StringToConvert); private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll(chars, new Converter(CharToByte)); return bytes; } private byte CharToByte(char ch) { return (byte)ch; } Hope it helps. Danny

      W 1 Reply Last reply
      0
      • D DannyAdler

        Hi there, Maybe this will put you in the right track: string StringToConvert = "abc"; //This call will return a byte array with the numeric values of the chars byte[] bytes = StringToByteArray(StringToConvert); private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll(chars, new Converter(CharToByte)); return bytes; } private byte CharToByte(char ch) { return (byte)ch; } Hope it helps. Danny

        W Offline
        W Offline
        worked on mine
        wrote on last edited by
        #3

        Hi Danny, This looks like what I'm after except I'm getting two errors when implemented: Error 1 Using the generic type 'System.Converter' requires '2' type arguments Error 2 The type arguments for method 'System.Array.ConvertAll(TInput[], System.Converter)' cannot be inferred from the usage. Try specifying the type arguments explicitly. What did I do wrong?

        D 2 Replies Last reply
        0
        • W worked on mine

          How do I get string data into a byte array, programmatically, without doing a hex conversion. i.e., byte[i] = (???)string.Substring(0,2); I need the programmatic equivelent of: byte[] bytes = {0x14, 0x55, 0x90, 0x00, 0x01}; or byte[i] = {0x + string} I can't make any of this work. Need Guru! TIA, Steve

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          byte[] byteData = System.Text.Encoding.ASCII.GetBytes("Some string i want an array of bytes for"); If ASCII isn't your chosen encoding there are other useful ones predefined such as UTF8

          1 Reply Last reply
          0
          • W worked on mine

            Hi Danny, This looks like what I'm after except I'm getting two errors when implemented: Error 1 Using the generic type 'System.Converter' requires '2' type arguments Error 2 The type arguments for method 'System.Array.ConvertAll(TInput[], System.Converter)' cannot be inferred from the usage. Try specifying the type arguments explicitly. What did I do wrong?

            D Offline
            D Offline
            DannyAdler
            wrote on last edited by
            #5

            Sorry, I guess the pasting went wrong, the function should look like this: private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll<char, byte>(chars, new Converter<char, byte>(CharToByte)); return bytes; }

            1 Reply Last reply
            0
            • W worked on mine

              Hi Danny, This looks like what I'm after except I'm getting two errors when implemented: Error 1 Using the generic type 'System.Converter' requires '2' type arguments Error 2 The type arguments for method 'System.Array.ConvertAll(TInput[], System.Converter)' cannot be inferred from the usage. Try specifying the type arguments explicitly. What did I do wrong?

              D Offline
              D Offline
              DannyAdler
              wrote on last edited by
              #6

              private byte[] StringToByteArray(string str) { char[] chars = str.ToCharArray(); byte[] bytes = Array.ConvertAll<char, byte>(chars, new Converter<char, byte>(CharToByte)); return bytes; }

              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