Converting array of longs to char []
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
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
Dunno how about converting a double to a string, such as stringstream.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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;
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
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++;
}
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
tux4life >Unportable rubbish which doesn't work.
adatapost>Oh!!! Buddy you should have to test this code before judge.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
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 ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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...
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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
Duki
Nearly a Posting Virtuoso
1,475 posts since Jun 2006
Reputation Points: 817
Solved Threads: 32
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 :)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243