473,378 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

unsigned short array to string

This is a beginner question but is there a better way to do this? I
have an array of unsigned shorts and need to convert it into a string
array with the numbers separated by spaces. I've started to make an
intermediate string of characters that represents the digits. Am I on
the right track? I know java so there must be a simple way in C++ that
I don't know about since I'm new to the C++ world. Thanks for the
help!

int i=0;
int j=0;
const int DataLengthPerChannel=4;
const int maxDigits = 8;
unsigned short dataOut[DataLengthPerChannel];
short digit[maxDigits];

//initialize data
for (i=0; i<DataLengthPerChannel; i++) {
dataOut[i]=200*i+74;
}
//first loop around every element of dataOut
//then loop around the digits of the numbers
for (j=0; j<DataLengthPerChannel; j++) {
for (i=maxDigits-1; i>=0; i--) {
digit[i] = dataOut[j]%10;
dataOut[j] /= 10;
}//for
}//for

Jul 23 '05 #1
2 9276
greyham wrote:
This is a beginner question but is there a better way to do this? I
have an array of unsigned shorts and need to convert it into a string
array with the numbers separated by spaces.
You need to get your spec straight. It's either a string array (where
each element is a representation of the respective element of the other
array) or it's a single string with numbers separated by spaces.

Take a look at 'ostringstream' class. You can simply output all your
shorts into one array and then get the string out of it.
[...]


V
Jul 23 '05 #2
greyham wrote:
This is a beginner question but is there a better way to do this? I
have an array of unsigned shorts and need to convert it into a string
array with the numbers separated by spaces. I've started to make an
intermediate string of characters that represents the digits. Am I
on the right track? I know java so there must be a simple way in C++
that I don't know about since I'm new to the C++ world. Thanks for
the help!


I'm not entirely sure I understand what you want, but if my guess is
correct, you're looking for something like this:

std::vector<short> dataout;
std::ostringstream inter;

// create the data
for (int i=0; i<DataLengthPerChannel; ++i)
dataout.push_back(200*i+74);

// format the data
std::copy(dataout.begin(), dataout.end(),
std::ostream_iterarator<short>(inter, " "));

// get the formatted output as a string.
std::string concatenated_output = inter.str();

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Anupam | last post by:
Hi, This was asked on our school newsgroup by a friend of mine. He gave the following code and said that it did not print out the elements of the array. He asked for an explanation for this...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
0
by: krishnacins | last post by:
Dear Sir, i want know how to change char or string into unsigned short. Actualy i want to store a string into 2 bytes data type variable of pointer. Like unsigned short s={'H','e','l','l','o',0};...
3
by: tutush | last post by:
Hi there, I have problem with importing my C++ code to C#. The c++ looks like these extern _declspec(dllexport) const char* SendAndReceiveBufferedWrp(__int64 hConnection, const char*...
5
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
0
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
5
by: sam.barker0 | last post by:
Hi, How can I convert an unsigned char array containing IPV6 address into a string Eg if arrray contains 20 01 05 03 a8 3e 00 00 00 00 00 00 00 02 00 30 Then the address is Addr:...
2
by: sam.barker0 | last post by:
Hi guys, I am trying to form an IPV6 address string from the address bytes contained in a unsigned char buffer char tempstring; sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.