943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 921
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 20th, 2009
0

Converting array of longs to char []

Expand Post »
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
Similar Threads
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
May 20th, 2009
0

Re: Converting array of longs to char []

Dunno how about converting a double to a string, such as stringstream.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 20th, 2009
0

Re: Converting array of longs to char []

I think this should answer your question
http://www.codeguru.com/forum/showthread.php?t=231056
Reputation Points: 28
Solved Threads: 4
Light Poster
_Nestor is offline Offline
43 posts
since Apr 2008
May 20th, 2009
0

Re: Converting array of longs to char []

So let me see if I understand this. If I use
C++ Syntax (Toggle Plain Text)
  1. template <class T>
  2. std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
  3. {
  4. std::ostringstream oss;
  5. oss << f << t;
  6. return oss.str();
  7. }

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

C++ Syntax (Toggle Plain Text)
  1. cout<<to_string<long>(123456, std::string)<<std::endl;
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
May 20th, 2009
-1

Re: Converting array of longs to char []

Click to Expand / Collapse  Quote originally posted by Duki ...
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.

C++ Syntax (Toggle Plain Text)
  1. char *p;
  2. long a[4]={1,1,1,1};
  3. int i=0;
  4. p=(char *)a;
  5.  
  6. while(i<16)
  7. {
  8. printf("\n%c",*(p+i));
  9. i++;
  10. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 20th, 2009
-1

Re: Converting array of longs to char []

tux4life >Unportable rubbish which doesn't work.
adatapost>Oh!!! Buddy you should have to test this code before judge.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 20th, 2009
0

Re: Converting array of longs to char []

Click to Expand / Collapse  Quote originally posted by adatapost ...
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 ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 20th, 2009
0

Re: Converting array of longs to char []

Click to Expand / Collapse  Quote originally posted by Duki ...
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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 20th, 2009
0

Re: Converting array of longs to char []

Click to Expand / Collapse  Quote originally posted by Duki ...
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...
Last edited by Nick Evan; May 20th, 2009 at 2:03 pm.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
May 20th, 2009
0

Re: Converting array of longs to char []

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
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Moving an object in a 2d-vector
Next Thread in C++ Forum Timeline: Shuffle The Order Of Numbers In 1-D Array!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC