Solved

Convert long to char

Posted on 2004-08-24
13
681 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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