Converting a unsigned char * to const char *

H

hamishd

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

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

Jim Langston

hamishd said:
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 );
 
?

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

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?
 
B

Barry

Jim said:
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
 
B

Barry

Barry said:
I think reinpterpret_cast does *NOT* work with constness
maybe static_cast and const_cast together will work

sorry, the title from the OP is misleading
 
B

Barry

Erik said:
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
 
I

Ian Collins

hamishd said:
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.
 
H

hamishd

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()
 
I

Ian Collins

hamishd said:
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)?
 
B

BobR

Ian Collins said:
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>
 
J

Jim Langston

hamishd said:
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*
 
B

BobR

hamishd said:
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
 
B

BobR

Jim Langston said:
hamishd said:
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 said:
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>).
// -------
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top