Solved

Convert long to char

Posted on 2004-08-24
13
692 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

808 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