By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

Converting a unsigned char * to const char *

P: n/a
Is this possible? Sorry if this question isn't relevant here.

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

Aug 4 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"hamishd" <Ha*********@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.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_cast is what you want.
reinterpret_cast<int>( Foo );
Aug 4 '07 #2

P: n/a
On 2007-08-04 11:47, Jim Langston wrote:
"hamishd" <Ha*********@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.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_cast is what you want.
reinterpret_cast<int>( Foo );
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?

--
Erik Wikström
Aug 4 '07 #3

P: n/a
Jim Langston wrote:
"hamishd" <Ha*********@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.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_cast is what you want.
reinterpret_cast<int>( Foo );

I think reinpterpret_cast does *NOT* work with constness
maybe static_cast and const_cast together will work
Aug 4 '07 #4

P: n/a
Erik Wikström wrote:
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?
you can assign unsigned char* to int

I think reinterpret_cast is fine here

Aug 4 '07 #5

P: n/a
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.
Aug 4 '07 #6

P: n/a
On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"hamishd" <Hamish.D...@gmail.comwrote in message

news:11**********************@x35g2000prf.googlegr oups.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_cast is what you want.
reinterpret_cast<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_cast<int>(ikey);

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


Aug 5 '07 #7

P: n/a
hamishd wrote:
On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"hamishd" <Hamish.D...@gmail.comwrote in message

news:11**********************@x35g2000prf.googleg roups.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_cast is what you want.
reinterpret_cast<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_cast<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<char*>(ikey)?

--
Ian Collins.
Aug 5 '07 #8

P: n/a

Ian Collins <ia******@hotmail.comwrote 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_cast<int>(uc) & 0xFF );

But it wouldn't be logical. <G>

--
Bob R
POVrookie
Aug 5 '07 #9

P: n/a
"hamishd" <Ha*********@gmail.comwrote in message
news:11*********************@e9g2000prf.googlegrou ps.com...
On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"hamishd" <Hamish.D...@gmail.comwrote in message

news:11**********************@x35g2000prf.googleg roups.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_cast is what you want.
reinterpret_cast<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_cast<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::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*
Aug 5 '07 #10

P: n/a

hamishd <Ha*********@gmail.comwrote in message...
On Aug 4, 6:47 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"hamishd" <Hamish.D...@gmail.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_cast is what you want.
reinterpret_cast<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_cast<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::stringstream sis;
sis << ikey;
int number;
sis >number;
std::cout<<"number = "<<number<<std::endl;
}
// out: number = 453

--
Bob R
POVrookie
Aug 5 '07 #11

P: n/a

Jim Langston <ta*******@rocketmail.comwrote in message...
"hamishd" <Ha*********@gmail.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_cast<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::stringstream convert;
convert << ikey;
int Value;
convert >value;
// std::string convert( ikey );

std::string Sconvert( ikey );
std::stringstream 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::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>).
// -------

--
Bob R
POVrookie
Aug 5 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.