473,382 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Aug 4 '07 #1
11 12701
"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
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
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
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
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
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
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

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
"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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
8
by: Simon | last post by:
Hi, I think this is slightly OT, (I am not certain that Macros are part of the standard), but I am hopping that someone could help. I am trying to use a language file in my system. the format...
10
by: Joachim Schmitz | last post by:
Hi folks Is it legal for a C compiler that claims to be conforming to the standard (c89) to issue an error on the following: char *foo(const char *s) { const char *s; for (s = src; *s &&...
11
by: Paul Emmons | last post by:
In writing a function similar to strstr(), I'm calling both of the arguments "const char *". My compiler (gcc) complains "warning: return discards qualifiers from pointer target type" unless I...
6
by: G Patel | last post by:
I've been looking at some code for string functions (certain implementation of them) and the non modified string is usually declared as a const char *s in the parameter list. I was wondering,...
2
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.