Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I am facing a problem

i have the following code

C++
#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>
using namespace std;

int main(){
    string key = "30 14 06 03 55 04 03 14 0D 2A";
    istringstream iss(key);
    vector<char> data;
    unsigned x;
    iss >> hex;
    while(iss >> x){
        data.push_back(x);
    }
    size_t size = data.size();
    cout << "char data[" << size << "];" << endl;
    for(int i=0;i < size ; ++i){
        cout << "data[" << i << "] = 0x" 
             << hex << uppercase << setw(2) << setfill('0') <<  (unsigned)data[i] << ';' << endl;
    }
}



i want the output to be like this
content[0] = 0x30;

content[1] = 0x14;

content[2] = 0x06;

content[3] = 0x03;

content[4] = 0x55;

content[5] = 0x04;

content[6] = 0x03;

content[7] = 0x14;

content[8] = 0x0D;

content[9] = 0x2A;


so i made changes in the for loop
C++
unsigned *content;
content = (unsigned) malloc(size);
 for(int i=0;i < size ; ++i){
        cout << "data[" << i << "] = 0x" 
             << hex << uppercase << setw(2) << setfill('0') <<  (unsigned)data[i] << ';' << endl;
		
		cout<<"a is" <<a<<endl;
		
		content[i]=data[i];
		cout<<"content[i] is" <<content[i]<<endl;


Then i realized i have done the 0x printing in cout only please help me in achieving the fainl output
Posted
Comments
Sergey Alexandrovich Kryukov 19-Mar-14 12:50pm    
You need to explain what do you want to achieve conceptually, not by examples. If the behavior of your code does not match what you expected, use the debugger and fix it. If you failed to do so, describe what did you want to achieve, what's expected behavior, what is observed behavior, why do you feel it's wrong. If you have some exceptions, describe steps to reproduce, complete exception information. Comment the points in code related to exception throwing/propagation. Post all what's relevant. See also: http://www.sscce.org.
—SA
Robert Clove 19-Mar-14 13:08pm    
I want the content array to have data as shown above
Sergey Alexandrovich Kryukov 19-Mar-14 13:14pm    
How about using the debugger and finding out what's going on? This is just work, not anything requiring an expert's advice...
—SA

1 solution

You just need to add an extra endl to each line, like:
C++
for(int i=0;i < size ; ++i)
{
    cout << "content[" << i << "] = 0x" << hex << uppercase << setw(2) << setfill('0') <<  (unsigned)data[i] << ';' << endl << endl;
}


[edit]
Added the extra endline.
[/edit]
 
Share this answer
 
v2
Comments
Robert Clove 19-Mar-14 14:04pm    
Richard sir you are printing the 0x in cout but i want that when i print content like for(int i=0;i < size ; ++i)
{
cout << "content[" << i << "]<<content[i] << ';' << endl;
}
the output should be like this

content[0] = 0x30;

content[1] = 0x14;

content[2] = 0x06;

content[3] = 0x03;

content[4] = 0x55;

content[5] = 0x04;

content[6] = 0x03;

content[7] = 0x14;

content[8] = 0x0D;

content[9] = 0x2A;
Richard MacCutchan 19-Mar-14 14:31pm    
Sorry, I missed out the extra endline, try it now.

I'm not sure what you mean by "you are printing the 0x in cout".
Robert Clove 19-Mar-14 14:45pm    
i want this output content[0] = 0x30; content[1] = 0x14; content[2] = 0x06; content[3] = 0x03; content[4] = 0x55; content[5] = 0x04; content[6] = 0x03; content[7] = 0x14; content[8] = 0x0D; content[9] = 0x2A; from the following statementfor(int i=0;i < size ; ++i) { cout << "content[" << i << "]<<content[i] << ';' << endl; }
Richard MacCutchan 19-Mar-14 16:28pm    
Well that is not what you asked for previously. Do you want it all on one line, on separate lines or on separate lines with double spacing? It should not be too difficult to modify the sample code that I provided above.
Robert Clove 20-Mar-14 0:08am    
sir plz provide pseudo code to get this output content[0] = 0x30; content[1] = 0x14; content[2] = 0x06; content[3] = 0x03; content[4] = 0x55; content[5] = 0x04; content[6] = 0x03; content[7] = 0x14; content[8] = 0x0D; content[9] = 0x2A; from the following statementfor(int i=0;i < size ; ++i) { cout << "content[" << i << "]<<content[i] << ';' << endl; }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900