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. byte array conversion

byte array conversion

Scheduled Pinned Locked Moved C#
helpdata-structuressales
4 Posts 2 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.
  • B Offline
    B Offline
    bttds
    wrote on last edited by
    #1

    From a windows application form I need to print a ticket from a special ticket printer. that ticket includes a 2D barcode which is a string of all the information about a customer. I need to encrypt it and get as a byte array so I use Cryptoservices and done that. to concatenate that data with other data I've done something like this System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); string str = System.Text.Encoding.ASCII.GetString(b); So printing part is working fine. But when I scan the barcode and try to get it back to byte array like this, byte[] b = System.Text.Encoding.ASCII.GetBytes(data); Then my array size gets smaller and when I try to decrypt it it throws an error saying that invalid length of string. can anybody help me.

    G 1 Reply Last reply
    0
    • B bttds

      From a windows application form I need to print a ticket from a special ticket printer. that ticket includes a 2D barcode which is a string of all the information about a customer. I need to encrypt it and get as a byte array so I use Cryptoservices and done that. to concatenate that data with other data I've done something like this System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); string str = System.Text.Encoding.ASCII.GetString(b); So printing part is working fine. But when I scan the barcode and try to get it back to byte array like this, byte[] b = System.Text.Encoding.ASCII.GetBytes(data); Then my array size gets smaller and when I try to decrypt it it throws an error saying that invalid length of string. can anybody help me.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can't use ASCII encoding to get the bytes as a string. It only support seven bit character codes, which means that any byte that is between 128 and 255 is replaced by a question mark. Actually, you shouldn't use any text encoding at all to turn arbitrary bytes into text. Most encodings does't have a character for every possible byte value. Text encodings are used to turn text into bytes and back, not the other way around. You have to encode the bytes in a different way. You can for example use base64 encoding: string str = Convert.ToBase64String(b); byte[] b = Convert.FromBase64String(str);

      --- single minded; short sighted; long gone;

      B 1 Reply Last reply
      0
      • G Guffa

        You can't use ASCII encoding to get the bytes as a string. It only support seven bit character codes, which means that any byte that is between 128 and 255 is replaced by a question mark. Actually, you shouldn't use any text encoding at all to turn arbitrary bytes into text. Most encodings does't have a character for every possible byte value. Text encodings are used to turn text into bytes and back, not the other way around. You have to encode the bytes in a different way. You can for example use base64 encoding: string str = Convert.ToBase64String(b); byte[] b = Convert.FromBase64String(str);

        --- single minded; short sighted; long gone;

        B Offline
        B Offline
        bttds
        wrote on last edited by
        #3

        when I use base64 its working fine. But i want to optimise the space taken by the barcode. I tried hex conversion and that also works fine. and then the size of the barcode is smaller. I want to make it much smaller.

        G 1 Reply Last reply
        0
        • B bttds

          when I use base64 its working fine. But i want to optimise the space taken by the barcode. I tried hex conversion and that also works fine. and then the size of the barcode is smaller. I want to make it much smaller.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Base64 puts 6 bits of data in each character, which only gives an overhead of 33%. That's the most compact way that you can easily represent binary data as text. Hexadeximal representation only puts 4 bits of data in each character, giving an overhead of 50%. Even if you find some complicated conversion that can squeeze almost 8 bits of data in each character, that is only about 30% better than base64. If you want to make the string substantially shorter, look at what the data represents and see if you can remove any unused parts of it.

          --- single minded; short sighted; long gone;

          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