Solved

Convert long to char

Posted on 2004-08-24
13
661 Views
Last Modified: 2008-01-09
Normally my function     char *StreamClient()
return a CHAR value, but in one case, it need to return a value stored in a LONG variable.

How can i convert a LONG variable into a char * ?


example of function:

char *StreamClient()
{
     LONG i=1;

     return ???? i;
}

Thankx
0
Comment
Question by:mammouth
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11885134
You could use

char *StreamClient()
{
    static acLong [ 12 ]; // –2147483647 is the 'longest' 'LONG' value /w 11 chars
    LONG i=1;
    sprintf ( acLong, "%ld". i);

    return acLong;
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11885172
The incorrect answer could be:

char *StreamClient()
{
      LONG i=1;
      char buffer[20];
      sprintf (buffer, "%i", i);
      return buffer;
}

It is incorrect because buffer will be destroyed once StreamClients ends.
A possible correct answer could be:

char *StreamClient()
{
      LONG i=1;
      char *buffer = new char[20];
      sprintf (buffer, "%i", i);
      return buffer;
}

buffer will not be destroyed as it has been created dinamically with 'new' operator.

But you will need to destroy this buffer after using with the 'delete' operator.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11885209
>>But you will need to destroy this buffer after using with the 'delete' operator.

Not necessary if you use a local buffer declared as 'static' :o)

BTW, the above should read

char *StreamClient()
{
   static char acLong [ 12 ]; // –2147483647 is the 'longest' 'LONG' value /w 11 chars
   LONG i=1;
   sprintf ( acLong, "%ld". i);

   return acLong;
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11885246
Static buffer will work OK if you don't call the function twice at the same time. Also you can use a global buffer with similar effect.
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 125 total points
ID: 11885258
The issue with jkr's solution is that you can only call StreamClient with one variable at a time.  If you have call StreamClient and assign the value to c1, for example, and then call it again and assign that value to c2, c1 and c2 will point to the same thing and you will lose the original value of c1.  Possibly an easier solution would be to send the char array in as a parameter:

char *StreamClient(char *acLong)
{
  LONG i = 1;
  sprintf(acLong, "%ld", i);
  return acLong; // Return it in case you are assigning it to multiple variables or using the stream insertion operator or whatever
}

Hope this helps.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11885287
I fully agree with mnashadka, it is the cleanest solution.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:mammouth
ID: 11885346
ok, but if i have 2 long that i need to separate with "-"
For example:

LONG i=123;
LONG j=456;

i need to return "123-456"

How?
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 11885364
sprintf("%ld-%ld", i, j);
return acLong;
0
 
LVL 1

Author Comment

by:mammouth
ID: 11892035
sprintf("%ld-%ld", i, j);   convert 2 LONG to a CHAR.
If i want to convert 2 LONG to a UNSIGNED CHAR, what will be the codeline to use?
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 11892072
Unsigned char arrays and char arrays are pretty much the same in just about every way.  You can just cast your unsigned char * to a char * to convert it:
sprintf((char *)acLong, "%ld-%ld", i, j);
0
 
LVL 1

Author Comment

by:mammouth
ID: 11892125
Thankx
0
 
LVL 1

Author Comment

by:mammouth
ID: 11892228
One last question.
In the sample below, if rep.response is a char[4], how can i put is content in buffer an unsigned char?

following code not working.
sprintf((char *)buffer, "%ld", rep.response);
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 11892261
If rep.reponse is a character pointer (as in this case), you could use sprintf with %s (instead of %ld), but it's much easier just to copy the contents using strcpy:
strcpy((char *)buffer, rep.response);
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now