I am trying to convert an array of longs to an array of chars. There are 1081 elements in the long array. Each element is 4 digits long. Is there a function in C++ that will convert a long something I can store in the array or could someone point me in the right direction? I'm not sure how to go about solving this one.

Thanks

Member Avatar for iamthwee

Dunno how about converting a double to a string, such as stringstream.

So let me see if I understand this. If I use

template <class T>
std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
{
  std::ostringstream oss;
  oss << f << t;
  return oss.str();
}

I should be able to pass this function a long and it will convert it to a string.

cout<<to_string<long>(123456, std::string)<<std::endl;

I am trying to convert an array of longs to an array of chars. There are 1081 elements in the long array. Each element is 4 digits long. Is there a function in C++ that will convert a long something I can store in the array or could someone point me in the right direction? I'm not sure how to go about solving this one.

Thanks

Yes, it is.

Here is a code.

char *p;
  long a[4]={1,1,1,1};
  int i=0;
  p=(char *)a;

  while(i<16)
   {
      printf("\n%c",*(p+i));
      i++;
   }
commented: Unportable rubbish which doesn't work :( -1

tux4life >Unportable rubbish which doesn't work.
adatapost>Oh!!! Buddy you should have to test this code before judge.

commented: Or you should shut up when you know you are not solving any purpose of the problem in hand. -1

tux4life >Unportable rubbish which doesn't work.
adatapost>Oh!!! Buddy you should have to test this code before judge.

I have tested it before I judged, I'm not stupid ...

I am trying to convert an array of longs to an array of chars. There are 1081 elements in the long array. Each element is 4 digits long. Is there a function in C++ that will convert a long something I can store in the array or could someone point me in the right direction? I'm not sure how to go about solving this one.

Perhaps you could try to clarify what it is you are attempting to do.

By each element, I assume you mean each long integer. By 4 digits long, do you mean the values are 1234 , 5678 , etc.?

By convert to char[], do you mean convert to a 5-element C-style string? To a std::string? Other?

Or do you mean you would like to display the underlying hexadecimal representation of the value -- such as the decimal 1234567890 being equivalent to 499602D2 in hexadecimal (4 bytes in this case)?

And, what is the larger picture? That is, have you chosen an assumed solution without fully understanding the problem?

I am trying to convert an array of longs to an array of chars. There are 1081 elements in the long array. Each element is 4 digits long.

So if I understand correct: These number vary from 0 to 9999?
A single char can only hold values up to 255, so it's not possible to hold the value in a single char.
If you want to convert each long to a char-array (so create a 2d array), you could use stringstreams as mentioned by iamthwee, or you could do it with bitwise operators. (or even sprintf....)

[edit] Dave beat me to it...

Sorry for the confusion. Let me try to clarify it a little. What I'm trying to do is to get two already written programs to work together. I have one program that gets data from a Laser Measurement System. It reports its values in an array of longs. I need to send these vaules to another program. I have some code that will send a char* via udp ports (using the sendto() function). So I thought that I could convert the each long to something that my code could send to the other program.

I may be doing this completely wrong, so if you know of a better way to do this please let me know. The only reason that I'm doing it this way is because of time constraints and limited knowledge of this type of programming.

Hope this helps

I would advice to use string streams (as already mentioned a couple of times):

const int N = 5; // number of elements in the long array
long a[N]={345,678,890,123,456}; // a simple long array
char str[N][5]; // the char array where we're going to store the converted longs in
stringstream ss; // our stringstream we're going to use for conversion

for(int i=0; i<N; i++)
    ss << a[i] << " "; // send all elements of the long array to the stringstream
for(int i=0; i<N; i++)
    ss >> str[i]; // take them back out the stream as a character string

Don't forget to include sstream :)

commented: Another fine option. +19

My choice would be sprintf .

#include <cstdio>

int main()
{
   long array[] = {1234,567890,-42};
   char text[12]; /* adjust to taste */
   for ( std::size_t i = 0; i < sizeof array / sizeof *array; ++i )
   {
      std::sprintf(text, "%ld", array[i]);
      std::puts(text); /* mimic a send */
   }
   return 0;
}
commented: Yeah, sprintf is a great function :) +5

Write an example on datainput, and dataoutput,
like


int inPut=1234;
char *output= \\how do I convert 1234 to char*

How about:

// A function to convert Long to String
string longToString(long In)
{
    ostringstream OutStream;
    
    OutStream << In;
    
    return (OutStream.str());
}

/* ...  */

/*
    This function traverses your array of Long Integers
    (MyLongArray), and converts each long integer into
    the corresponding string values and stores them in
    the MyLongStrings Array. Works like a charm :)
*/
for(int i = 0; i < LongArrayIndex; i++)
{
    MyLongStrings[i] = longToString(MyLongArray[i]);
}

/* ... */

Anyway, Hope this helped!

Also possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.