473,378 Members | 1,067 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,378 software developers and data experts.

need help with casting

175 100+
I'm trying to cast a char* to a long ... but I keep getting compiler errors no matter how I do it ... here's the small sample I've been messing with. I just have console input, where I'll enter an integer ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. char x[15];    // or char* x;  -- I'd like to be able to do both
  3. long longResult;
  4.  
  5. // these both produce a jumbled result
  6. longResult = (long)x;
  7.  
  8. longResult = reinterpret_cast<int> (x);
  9.  
  10. // this one gives the error -- cannot convert from 'char *' to 'long
  11. longResult = static_cast<long> (x);                
  12.  
  13.  
... i apparantly have no idea what I'm doing, so I'd be grateful for any help.
Oct 17 '07 #1
2 1069
Banfa
9,065 Expert Mod 8TB
Do not use a C style cast in C++ because you can not be sure what underlying cast the cmpiler will have chosen to do. It is only there for support of C code.

The reinterpret_cast is the one you want for this, this tells the compiler to interpreate the bits of one type as another type, not performing any sort of conversion.

The static_cast converts 1 type to another, since there are no pointer to integer converstion the compiler produces an error.

The output of the reinterpret_cast is the address of the string as a number, you describe this as a jumbled result. This leads me to think that may be you are trying to convert a string of digits to a number, i.e. get the value the string "1234" as a long type.

You could do this like this

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     char data [] = "1234";
  9.     long result;
  10.  
  11.     stringstream converter(data);
  12.  
  13.     converter >> result;
  14.  
  15.     cout << result << endl;
  16.  
  17.     return 0;
  18. }
Oct 17 '07 #2
manontheedge
175 100+
Banfa,

thank you for the explanations, and for that sample code. That's exactly what I'm trying to do. I'm sending multiple data accross a network, and I chose to send them all as strings, but I need to pick out numbers in some of them.

Your code has some things I've never seen, so I'll have to look that up. Anyway, thank you very much for your help ... saved me a lot of time and stress.
Oct 17 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python...
13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
6
by: Keith Selbee | last post by:
I have a file that provides details on which user control to populate a web page with at runtime. I'm able to create and load the control successfully, but now I need to cast it from Control to...
3
by: David P. Donahue | last post by:
I'm trying to come up with the best (fastest, as this code will be run often) way to accomplish the following: I have a dynamic string array X (as one grabbed from a GetFiles() or...
2
by: Don | last post by:
Can someone help me fix my DLookup problem. I'm far from proficiency with Access. I've been creating databases for several years for work with the help of many of you and trial and error. I have...
8
by: SpreadTooThin | last post by:
Basically I think the problem is in converting from a 32 bit integer to a 16 bit integer. I have two arrays: import array a = array.array('L', ) b = array.array('H', ) b = a
1
by: madumm | last post by:
Hi all I really need a solution for the following:: ------------------------------------------------------------------------------------- Say i have a font object called 'myFont' as shown below;...
171
by: Raman | last post by:
Hi All, Here is a small Code, int main(void) { char *p=(char *) malloc(100); strcpy(p,"Test1234567890"); p=p+10; free(p);
0
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.