Converting a unsigned char * to const char *

Discussion in 'C++' started by hamishd, Aug 4, 2007.

  1. hamishd

    hamishd Guest

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

    actually, I'm really trying to convert a unsigned char * to an int
     
    hamishd, Aug 4, 2007
    #1
    1. Advertisements

  2. hamishd

    Jim Langston Guest

    Are you trying to convert the value of the pointer, or where the pointer is
    pointing to?

    Either way, reinterpret_cast is what you want.
    reinterpret_cast<int>( Foo );
     
    Jim Langston, Aug 4, 2007
    #2
    1. Advertisements

  3. reinterpret_cast 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?
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 4, 2007
    #3
  4. hamishd

    Barry Guest

    I think reinpterpret_cast does *NOT* work with constness
    maybe static_cast and const_cast together will work
     
    Barry, Aug 4, 2007
    #4
  5. hamishd

    Barry Guest

    sorry, the title from the OP is misleading
     
    Barry, Aug 4, 2007
    #5
  6. hamishd

    Barry Guest

    you can assign unsigned char* to int

    I think reinterpret_cast is fine here
     
    Barry, Aug 4, 2007
    #6
  7. hamishd

    Ian Collins Guest

    There isn't a portable way to do this, on many systems a char* is bigger
    than an int.
     
    Ian Collins, Aug 4, 2007
    #7
  8. hamishd

    hamishd Guest

    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_cast<int>(ikey);

    This does not work.
    The reason i asked about converting to a constr char * is that I
    wanted to use atoi()
     
    hamishd, Aug 5, 2007
    #8
  9. hamishd

    Ian Collins Guest

    What does RegQueryValueEx do to the ikey parameter?
    Why should it?
    Why not just use static_cast<char*>(ikey)?
     
    Ian Collins, Aug 5, 2007
    #9
  10. hamishd

    BobR Guest

    unsigned char *uc( 0 );
    int int16bit( reinterpret_cast<int>(uc) & 0xFF );

    But it wouldn't be logical. <G>
     
    BobR, Aug 5, 2007
    #10
  11. hamishd

    Jim Langston Guest

    Oh. You want to convert "453" to an int. I would use stringstream, which
    is an alternative to atoi

    std::stringstream 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*
     
    Jim Langston, Aug 5, 2007
    #11
  12. hamishd

    BobR Guest

    Why?

    #include <iostream>
    #include <sstream>

    { // main() or ?
    unsigned char ikey[256] = "453\0";
    std::stringstream sis;
    sis << ikey;
    int number;
    sis >> number;
    std::cout<<"number = "<<number<<std::endl;
    }
    // out: number = 453
     
    BobR, Aug 5, 2007
    #12
  13. hamishd

    BobR Guest

    // > std::string convert( ikey );

    std::string Sconvert( ikey );
    std::stringstream convert( Sconvert );

    It does for 'char*', but not for 'unsigned char*' (not on my old MinGW).

    // -------
    char const scc[] = "453\0";
    std::istringstream is( scc ); // no problem
    // -------
    unsigned char const scc[] = "453\0";
    std::istringstream is( scc );
    // error: invalid conversion from `const unsigned char*' to `
    // const char*'
    // error: initializing argument 1 of std::basic_string...
    (again, the casts got real ugly! <G>).
    // -------
     
    BobR, Aug 5, 2007
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.