Solved

Convert long to char

Posted on 2004-08-24
13
707 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

Independent Software Vendors: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

707 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