Solved

Convert long to char

Posted on 2004-08-24
13
699 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

685 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