Converting a unsigned char * to const char *

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hamishd

    Converting a unsigned char * to const char *

    Is this possible? Sorry if this question isn't relevant here.

    actually, I'm really trying to convert a unsigned char * to an int

  • Jim Langston

    #2
    Re: Converting a unsigned char * to const char *

    "hamishd" <Hamish.Dean@gm ail.comwrote in message
    news:1186219573 .640775.272620@ x35g2000prf.goo glegroups.com.. .
    Is this possible? Sorry if this question isn't relevant here.
    >
    actually, I'm really trying to convert a unsigned char * to an int
    Are you trying to convert the value of the pointer, or where the pointer is
    pointing to?

    Either way, reinterpret_cas t is what you want.
    reinterpret_cas t<int>( Foo );


    Comment

    • =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

      #3
      Re: Converting a unsigned char * to const char *

      On 2007-08-04 11:47, Jim Langston wrote:
      "hamishd" <Hamish.Dean@gm ail.comwrote in message
      news:1186219573 .640775.272620@ x35g2000prf.goo glegroups.com.. .
      >Is this possible? Sorry if this question isn't relevant here.
      >>
      >actually, I'm really trying to convert a unsigned char * to an int
      >
      Are you trying to convert the value of the pointer, or where the pointer is
      pointing to?
      >
      Either way, reinterpret_cas t is what you want.
      reinterpret_cas t<int>( Foo );
      reinterpret_cas t should not be needed it it's a unsigned char -int
      conversion the OP is trying to do, both are integer types so a normal
      assignment should do, right?

      --
      Erik Wikström

      Comment

      • Barry

        #4
        Re: Converting a unsigned char * to const char *

        Jim Langston wrote:
        "hamishd" <Hamish.Dean@gm ail.comwrote in message
        news:1186219573 .640775.272620@ x35g2000prf.goo glegroups.com.. .
        >Is this possible? Sorry if this question isn't relevant here.
        >>
        >actually, I'm really trying to convert a unsigned char * to an int
        >
        Are you trying to convert the value of the pointer, or where the pointer is
        pointing to?
        >
        Either way, reinterpret_cas t is what you want.
        reinterpret_cas t<int>( Foo );
        >
        >
        I think reinpterpret_ca st does *NOT* work with constness
        maybe static_cast and const_cast together will work

        Comment

        • Barry

          #5
          Re: Converting a unsigned char * to const char *

          Erik Wikström wrote:
          reinterpret_cas t should not be needed it it's a unsigned char -int
          conversion the OP is trying to do, both are integer types so a normal
          assignment should do, right?
          >
          you can assign unsigned char* to int

          I think reinterpret_cas t is fine here

          Comment

          • Ian Collins

            #6
            Re: Converting a unsigned char * to const char *

            hamishd wrote:
            Is this possible? Sorry if this question isn't relevant here.
            >
            actually, I'm really trying to convert a unsigned char * to an int
            >
            There isn't a portable way to do this, on many systems a char* is bigger
            than an int.

            --
            Ian Collins.

            Comment

            • hamishd

              #7
              Re: Converting a unsigned char * to const char *

              On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
              "hamishd" <Hamish.D...@gm ail.comwrote in message
              >
              news:1186219573 .640775.272620@ x35g2000prf.goo glegroups.com.. .
              >
              Is this possible? Sorry if this question isn't relevant here.
              >
              actually, I'm really trying to convert a unsigned char * to an int
              >
              Are you trying to convert the value of the pointer, or where the pointer is
              pointing to?
              >
              Either way, reinterpret_cas t is what you want.
              reinterpret_cas t<int>( Foo );
              Below is exactly what I'm doing. I want to read a value from the
              registry. Let's say the registry value is "453".

              unsigned char ikey[256];
              ULONG ilen = 256;
              HKEY ihKey;

              RegQueryValueEx (ihKey, "Value", 0, NULL, ikey, &ilen);

              But now ikey is not an int, i want an int of value 453.

              int RegistryVal = reinterpret_cas t<int>(ikey);

              This does not work.
              The reason i asked about converting to a constr char * is that I
              wanted to use atoi()




              Comment

              • Ian Collins

                #8
                Re: Converting a unsigned char * to const char *

                hamishd wrote:
                On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
                >"hamishd" <Hamish.D...@gm ail.comwrote in message
                >>
                >news:118621957 3.640775.272620 @x35g2000prf.go oglegroups.com. ..
                >>
                >>Is this possible? Sorry if this question isn't relevant here.
                >>actually, I'm really trying to convert a unsigned char * to an int
                >Are you trying to convert the value of the pointer, or where the pointer is
                >pointing to?
                >>
                >Either way, reinterpret_cas t is what you want.
                >reinterpret_ca st<int>( Foo );
                >
                Below is exactly what I'm doing. I want to read a value from the
                registry. Let's say the registry value is "453".
                >
                unsigned char ikey[256];
                ULONG ilen = 256;
                HKEY ihKey;
                >
                RegQueryValueEx (ihKey, "Value", 0, NULL, ikey, &ilen);
                >
                But now ikey is not an int, i want an int of value 453.
                >
                What does RegQueryValueEx do to the ikey parameter?
                int RegistryVal = reinterpret_cas t<int>(ikey);
                >
                This does not work.
                Why should it?
                The reason i asked about converting to a constr char * is that I
                wanted to use atoi()
                >
                Why not just use static_cast<cha r*>(ikey)?

                --
                Ian Collins.

                Comment

                • BobR

                  #9
                  Re: Converting a unsigned char * to const char *


                  Ian Collins <ian-news@hotmail.co mwrote in message...
                  hamishd wrote:
                  Is this possible? Sorry if this question isn't relevant here.
                  actually, I'm really trying to convert a unsigned char * to an int
                  There isn't a portable way to do this, on many systems a char* is bigger
                  than an int.
                  >
                  unsigned char *uc( 0 );
                  int int16bit( reinterpret_cas t<int>(uc) & 0xFF );

                  But it wouldn't be logical. <G>

                  --
                  Bob R
                  POVrookie


                  Comment

                  • Jim Langston

                    #10
                    Re: Converting a unsigned char * to const char *

                    "hamishd" <Hamish.Dean@gm ail.comwrote in message
                    news:1186272165 .221239.20150@e 9g2000prf.googl egroups.com...
                    On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
                    >"hamishd" <Hamish.D...@gm ail.comwrote in message
                    >>
                    >news:118621957 3.640775.272620 @x35g2000prf.go oglegroups.com. ..
                    >>
                    Is this possible? Sorry if this question isn't relevant here.
                    >>
                    actually, I'm really trying to convert a unsigned char * to an int
                    >>
                    >Are you trying to convert the value of the pointer, or where the pointer
                    >is
                    >pointing to?
                    >>
                    >Either way, reinterpret_cas t is what you want.
                    >reinterpret_ca st<int>( Foo );
                    >
                    Below is exactly what I'm doing. I want to read a value from the
                    registry. Let's say the registry value is "453".
                    >
                    unsigned char ikey[256];
                    ULONG ilen = 256;
                    HKEY ihKey;
                    >
                    RegQueryValueEx (ihKey, "Value", 0, NULL, ikey, &ilen);
                    >
                    But now ikey is not an int, i want an int of value 453.
                    >
                    int RegistryVal = reinterpret_cas t<int>(ikey);
                    >
                    This does not work.
                    The reason i asked about converting to a constr char * is that I
                    wanted to use atoi()
                    Oh. You want to convert "453" to an int. I would use stringstream, which
                    is an alternative to atoi

                    std::stringstre am convert;
                    convert << ikey;
                    int Value;
                    convert >value;

                    std::string convert( ikey );
                    int Value;
                    convert >Value;

                    may also work. Not sure if stringstream has a constructor taking a char*


                    Comment

                    • BobR

                      #11
                      Re: Converting a unsigned char * to const char *


                      hamishd <Hamish.Dean@gm ail.comwrote in message...
                      On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
                      "hamishd" <Hamish.D...@gm ail.comwrote in message
                      Is this possible? Sorry if this question isn't relevant here.
                      actually, I'm really trying to convert a unsigned char * to an int
                      Are you trying to convert the value of the pointer, or where the pointer
                      is
                      pointing to?
                      Either way, reinterpret_cas t is what you want.
                      reinterpret_cas t<int>( Foo );
                      >
                      Below is exactly what I'm doing. I want to read a value from the
                      registry. Let's say the registry value is "453".
                      >
                      unsigned char ikey[256];
                      ULONG ilen = 256;
                      HKEY ihKey;
                      RegQueryValueEx (ihKey, "Value", 0, NULL, ikey, &ilen);
                      >
                      But now ikey is not an int, i want an int of value 453.
                      >
                      int RegistryVal = reinterpret_cas t<int>(ikey);
                      This does not work.
                      The reason i asked about converting to a constr char * is that I
                      wanted to use atoi()
                      Why?

                      #include <iostream>
                      #include <sstream>

                      { // main() or ?
                      unsigned char ikey[256] = "453\0";
                      std::stringstre am sis;
                      sis << ikey;
                      int number;
                      sis >number;
                      std::cout<<"num ber = "<<number<<std: :endl;
                      }
                      // out: number = 453

                      --
                      Bob R
                      POVrookie


                      Comment

                      • BobR

                        #12
                        Re: Converting a unsigned char * to const char *


                        Jim Langston <tazmaster@rock etmail.comwrote in message...
                        "hamishd" <Hamish.Dean@gm ail.comwrote in message...
                        Below is exactly what I'm doing. I want to read a value from the
                        registry. Let's say the registry value is "453".

                        unsigned char ikey[256];
                        ULONG ilen = 256;
                        HKEY ihKey;
                        RegQueryValueEx (ihKey, "Value", 0, NULL, ikey, &ilen);
                        But now ikey is not an int, i want an int of value 453.
                        int RegistryVal = reinterpret_cas t<int>(ikey);
                        This does not work.
                        The reason i asked about converting to a constr char * is that I
                        wanted to use atoi()
                        >
                        Oh. You want to convert "453" to an int. I would use stringstream, which
                        is an alternative to atoi
                        >
                        std::stringstre am convert;
                        convert << ikey;
                        int Value;
                        convert >value;
                        >
                        // std::string convert( ikey );

                        std::string Sconvert( ikey );
                        std::stringstre am convert( Sconvert );

                        Nope, I tried it. (and the casts got real ugly! <G>).
                        int Value;
                        convert >Value;
                        >
                        may also work. Not sure if stringstream has a constructor taking a char*
                        It does for 'char*', but not for 'unsigned char*' (not on my old MinGW).

                        // -------
                        char const scc[] = "453\0";
                        std::istringstr eam is( scc ); // no problem
                        // -------
                        unsigned char const scc[] = "453\0";
                        std::istringstr eam is( scc );
                        // error: invalid conversion from `const unsigned char*' to `
                        // const char*'
                        // error: initializing argument 1 of std::basic_stri ng...
                        (again, the casts got real ugly! <G>).
                        // -------

                        --
                        Bob R
                        POVrookie


                        Comment

                        Working...