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 Convert C++ unsigned char * to C# byte[]

How to Convert C++ unsigned char * to C# byte[]

Scheduled Pinned Locked Moved C#
csharpc++helptutorialquestion
5 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.
  • J Offline
    J Offline
    Joe Rozario
    wrote on last edited by
    #1

    Dear friends , I have Problem like these ... i have C++ dll which returns "unsigned char *" - this contains image data now i want that to be converted to c# "byte[]" how to do that??? thank you

    M L 2 Replies Last reply
    0
    • J Joe Rozario

      Dear friends , I have Problem like these ... i have C++ dll which returns "unsigned char *" - this contains image data now i want that to be converted to c# "byte[]" how to do that??? thank you

      M Offline
      M Offline
      Michel Godfroid
      wrote on last edited by
      #2

      unsigned char * is equivalent to char[]; you can cast your return value to char[], or byte [], or byte *.

      J 1 Reply Last reply
      0
      • M Michel Godfroid

        unsigned char * is equivalent to char[]; you can cast your return value to char[], or byte [], or byte *.

        J Offline
        J Offline
        Joe Rozario
        wrote on last edited by
        #3

        Dear friend thank for the reply and few clarification, "unsigned char *" is a pointer array and is it possible to assign to simple char[] of c# without any address as such ?? unsigned char* buffer = new unsigned char[xSize*ySzie]; //Type in C++ byte[] ImageData =buffer; //C# throws error as "Cannot implicitly convert type 'byte*' to 'byte[]", do i need to change byte[] ImageData to byte[]* ImageData Thank you

        M 1 Reply Last reply
        0
        • J Joe Rozario

          Dear friend thank for the reply and few clarification, "unsigned char *" is a pointer array and is it possible to assign to simple char[] of c# without any address as such ?? unsigned char* buffer = new unsigned char[xSize*ySzie]; //Type in C++ byte[] ImageData =buffer; //C# throws error as "Cannot implicitly convert type 'byte*' to 'byte[]", do i need to change byte[] ImageData to byte[]* ImageData Thank you

          M Offline
          M Offline
          Michel Godfroid
          wrote on last edited by
          #4

          Sorry, my mistake. I saw your question and assumed it was a C++ question, so the answer I gave you was for C++. Have look at this: http://www.devnewsgroups.net/dotnetframework/t1416-unsafe-code-converting-byte-byte.aspx[^] I'll remove my previous answer...

          1 Reply Last reply
          0
          • J Joe Rozario

            Dear friends , I have Problem like these ... i have C++ dll which returns "unsigned char *" - this contains image data now i want that to be converted to c# "byte[]" how to do that??? thank you

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, you can't convert an unsigned char * (or any other pointer) to a byte[] or any other kind of array, as an array has a length which is not available when all you have is a pointer. the easiest way to transfer an array of elements between managed and unmanaged worlds is by having the managed world create the array and the native side to read/write said array. Which means the array's size must be known beforehand, and the array should be passed as a parameter. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Prolific encyclopedia fixture proof-reader browser patron addict?
            We all depend on the beast below.


            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