Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,557 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,913 people online right now. Registration is fast and FREE... Join Now!




How to convert char* to char in safer way?

 
Reply to this topicStart new topic

How to convert char* to char in safer way?

cyberkid
12 Apr, 2008 - 12:20 AM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2008
Posts: 1

I have a problem is converting char* to char. This is my code...

CODE


int n;
    char output[256];
    char srcport1[256],srcport2[256],dstport1[256],dstport2[256];
    
    stack<char*> port1;
    
    
    
    cout<<"Protocol Number:";
    cin>>n;  
    sprintf(output,"%x",n);
    cout<<output<<endl;
    cout<<strlen(output)<<endl;
  
    
     if(strlen(output)< 4 )
     {
        port1.push(output);
     for(int i=0; i< (4 - strlen(output)); i++)
     {
                  
        port1.push("0");    
            
     }
    }
    
   for(int i =0; i < 2; i++)
    {
      
       srcport1[i] = port1.top();
       port1.pop();                
                      
    }
    
   for(int i =0; i < 2; i++)
    {
      
       srcport2[i] = port1.top();
       port1.pop();                
                      
    }
    



It says " C:\Documents and Settings\Mahendran Armugam\Desktop\COMPUTER NETWORKS ASSIGMENT\netdump.cpp invalid conversion from `char*' to `char' " and when i tried this..

CODE


for(int i =0; i < 2; i++)
    {
      
       srcport2[i] = (char)port1.top(); //
       port1.pop();                
                      
    }


  cout<<srcport2<<endl; //this displays some garbage value (¶¶>?@abcÉ☺=)



Can somebody help me out please...
Thanks...
User is offlineProfile CardPM
+Quote Post

KYA
RE: How To Convert Char* To Char In Safer Way?
12 Apr, 2008 - 01:31 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
Can you make your stack of regular char? Without seeing some more code, I can't comment further. Where is your stack class defined?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: How To Convert Char* To Char In Safer Way?
12 Apr, 2008 - 08:13 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 50 times
Dream Kudos: 550
My Contributions
if you using C++ you really should be using strings rather than char*
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:21PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month