| | |
Converting array of longs to char []
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
-Edsger Dijkstra
•
•
Join Date: Apr 2008
Posts: 38
Reputation:
Solved Threads: 4
I think this should answer your question
http://www.codeguru.com/forum/showthread.php?t=231056
http://www.codeguru.com/forum/showthread.php?t=231056
So let me see if I understand this. If I use
I should be able to pass this function a long and it will convert it to a string.
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
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
-Edsger Dijkstra
•
•
•
•
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)
char *p; long a[4]={1,1,1,1}; int i=0; p=(char *)a; while(i<16) { printf("\n%c",*(p+i)); i++; }
Failure is not fatal, but failure to change might be. - John Wooden
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
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.
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
•
•
•
•
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.
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.
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 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
-Edsger Dijkstra
![]() |
Similar Threads
- Safe Array (C++)
- Copy a string to a 2d array of type char (C++)
- Two-dimensional char array (C++)
- Help needed :( (C++)
- Converting array of chars to int (C)
- converting a char array to a single char (C)
- adding data into an char array (C++)
Other Threads in the C++ Forum
- Previous Thread: Moving an object in a 2d-vector
- Next Thread: Shuffle The Order Of Numbers In 1-D Array!!!!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






