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 / C++ / MFC
  4. How to cast const char * to unsigned char * ?

How to cast const char * to unsigned char * ?

Scheduled Pinned Locked Moved C / C++ / MFC
jsontutorialquestion
6 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.
  • M Offline
    M Offline
    Mr Brainley
    wrote on last edited by
    #1

    Take the function void foo(unsigned char * param). It does not change the value of the string (it's a Win32 API function, don't ask me why it's not const). Now i'd like to pass it the value of std::string MyString. I do this liek that

    foo(reinterpret_cast<unsigned char *>(MyString.c_str()));

    but this can hardly be the recommended way (it works, but it's bad). Any suggestions anyone ?

    T C 2 Replies Last reply
    0
    • M Mr Brainley

      Take the function void foo(unsigned char * param). It does not change the value of the string (it's a Win32 API function, don't ask me why it's not const). Now i'd like to pass it the value of std::string MyString. I do this liek that

      foo(reinterpret_cast<unsigned char *>(MyString.c_str()));

      but this can hardly be the recommended way (it works, but it's bad). Any suggestions anyone ?

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      why not the simple static_cast ? i can't see anything wrong otherwise...


      You don't know where to start ? ask a good friend

      [VisualCalc 3.0][Flags Beginner's Guide]

      M 1 Reply Last reply
      0
      • T toxcct

        why not the simple static_cast ? i can't see anything wrong otherwise...


        You don't know where to start ? ask a good friend

        [VisualCalc 3.0][Flags Beginner's Guide]

        M Offline
        M Offline
        Mr Brainley
        wrote on last edited by
        #3

        static_cast gives a compiler error:

        error C2440: 'static_cast' : cannot convert from 'const char *' to 'unsigned char *'

        T R 2 Replies Last reply
        0
        • M Mr Brainley

          static_cast gives a compiler error:

          error C2440: 'static_cast' : cannot convert from 'const char *' to 'unsigned char *'

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          hum, yes, that's right (i have no compiler with me at the moment...). does it work without any explicit cast ?


          You don't know where to start ? ask a good friend

          [VisualCalc 3.0][Flags Beginner's Guide]

          1 Reply Last reply
          0
          • M Mr Brainley

            Take the function void foo(unsigned char * param). It does not change the value of the string (it's a Win32 API function, don't ask me why it's not const). Now i'd like to pass it the value of std::string MyString. I do this liek that

            foo(reinterpret_cast<unsigned char *>(MyString.c_str()));

            but this can hardly be the recommended way (it works, but it's bad). Any suggestions anyone ?

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            You want const_cast.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            1 Reply Last reply
            0
            • M Mr Brainley

              static_cast gives a compiler error:

              error C2440: 'static_cast' : cannot convert from 'const char *' to 'unsigned char *'

              R Offline
              R Offline
              Roger Stoltz
              wrote on last edited by
              #6

              You cannot cast a 'const' value to a 'non-const' because that's the whole point of the 'const' keyword. In this case you have to make a copy of the string and provide the second function with the copy. -- Roger


              "It's supposed to be hard, otherwise anybody could do it!" - selfquote

              "No one remembers a coward!" - Jan Elfström 1998
              "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above

              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