Converting array of longs to char []

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Converting array of longs to char []

 
0
  #1
May 20th, 2009
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
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Converting array of longs to char []

 
0
  #2
May 20th, 2009
Dunno how about converting a double to a string, such as stringstream.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 38
Reputation: _Nestor is an unknown quantity at this point 
Solved Threads: 4
_Nestor _Nestor is offline Offline
Light Poster

Re: Converting array of longs to char []

 
0
  #3
May 20th, 2009
I think this should answer your question
http://www.codeguru.com/forum/showthread.php?t=231056
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Converting array of longs to char []

 
0
  #4
May 20th, 2009
So let me see if I understand this. If I use
  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.

  1. cout<<to_string<long>(123456, std::string)<<std::endl;
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Converting array of longs to char []

 
-1
  #5
May 20th, 2009
Originally Posted by Duki View 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

Yes, it is.

Here is a code.

  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. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Converting array of longs to char []

 
-1
  #6
May 20th, 2009
tux4life >Unportable rubbish which doesn't work.
adatapost>Oh!!! Buddy you should have to test this code before judge.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Converting array of longs to char []

 
0
  #7
May 20th, 2009
Originally Posted by adatapost View Post
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 ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Converting array of longs to char []

 
0
  #8
May 20th, 2009
Originally Posted by Duki View 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.
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Converting array of longs to char []

 
0
  #9
May 20th, 2009
Originally Posted by Duki View 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.
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 niek_e; May 20th, 2009 at 2:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Converting array of longs to char []

 
0
  #10
May 20th, 2009
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
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC