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. Converting ASCII text to a string

Converting ASCII text to a string

Scheduled Pinned Locked Moved C#
csharpc++tutorialquestion
3 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
    benglish72
    wrote on last edited by
    #1

    Hey Guys, bit of a C# newby, so if you could point me to a relevant article, or give a few tips would me much appreciated. I've got a file, which I load and decompress to ASCII text in a C++ dll (would have no idea how to do it in C#, unless there's a zip library for C#) and can import that into C# like: byte * pBuffer=0; int bytes; if(DodgyImportFunction(sourceFile,&pBuffer,&bytes)) { if(bytes>0) { //at this point have a pointer to a bytes byte*, but I need it as //byte[] and C# won't be nice and let me cast it, so I can use it as //ASCII text. } } Any ideas of a better solution so I can get the decompressed data to be treated as ASCII text in C#? Thanks, Brian.

    J 1 Reply Last reply
    0
    • B benglish72

      Hey Guys, bit of a C# newby, so if you could point me to a relevant article, or give a few tips would me much appreciated. I've got a file, which I load and decompress to ASCII text in a C++ dll (would have no idea how to do it in C#, unless there's a zip library for C#) and can import that into C# like: byte * pBuffer=0; int bytes; if(DodgyImportFunction(sourceFile,&pBuffer,&bytes)) { if(bytes>0) { //at this point have a pointer to a bytes byte*, but I need it as //byte[] and C# won't be nice and let me cast it, so I can use it as //ASCII text. } } Any ideas of a better solution so I can get the decompressed data to be treated as ASCII text in C#? Thanks, Brian.

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      Copy it into a string using Marshal.PtrToStringAnsi().

      //declare your function like this:
      private static extern bool DodgyImportFunction(
      string sourcefile, //don't know what datatype this was
      ref IntPtr buffer,
      ref int bytes
      );

      IntPtr pBuffer=IntPtr.Zero;
      int bytes;

      if(DodgyImportFunction(sourceFile,ref pBuffer,ref bytes))
      {
      if(bytes>0)
      {
      string str=Marshal.PtrToStringAnsi(pBuffer,bytes);
      //'str' should now be a managed string containing
      //the text from pBuffer
      }
      }

      "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
      "You must be the change you wish to see in the world." - Mahatma Gandhi

      B 1 Reply Last reply
      0
      • J J Dunlap

        Copy it into a string using Marshal.PtrToStringAnsi().

        //declare your function like this:
        private static extern bool DodgyImportFunction(
        string sourcefile, //don't know what datatype this was
        ref IntPtr buffer,
        ref int bytes
        );

        IntPtr pBuffer=IntPtr.Zero;
        int bytes;

        if(DodgyImportFunction(sourceFile,ref pBuffer,ref bytes))
        {
        if(bytes>0)
        {
        string str=Marshal.PtrToStringAnsi(pBuffer,bytes);
        //'str' should now be a managed string containing
        //the text from pBuffer
        }
        }

        "Blessed are the peacemakers, for they shall be called sons of God." - Jesus
        "You must be the change you wish to see in the world." - Mahatma Gandhi

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

        Thanks heaps, that worked a treat!:) You ripper, would've taken me ages to work that out. You beauty!

        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