Problem with converting a char * to string

Hi Guys,

I have a char * array which contains integer values. I want to convert this into string so as to make use of string class functions. The code is like this

char *arrayT;
arrayT = new char[100];

I store integer vales in arrayT. But when I do the following I don't see any output.

string str(arrayT);
cout << str;

The output would display an empty space. I do not understand what's wrong with this. I wrote a smaple program where in

char arrayT[] = {'a','b','c','\0'};

When I do the following I get the correct output.

string str(arrayT);
cout<< str;

output: abc

When this works fine whats wrong with the above code ?

Thank you
Try using stringstream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <sstream>
#include <iostream>

#using namespace std

int main()
{
    char* foo = "12345";
    string bar;

    stringstream ss;
    ss << foo;
    ss >> bar;

    cout << bar << endl;

    return 0;
}


I didn't actually check this code in a compiler, so there may be an issue or two, but this gives you the general idea.

because your first string is empty..
string class copy till it finds a \0. as the first string is empty it will not copy anything and display empty string.
Hi GenTiradentes

I did what you told me and it does work for the example you gave me. But, it doesn't work for my program. I have a char * array which contains elements like

arrayT[0] = 0;
arrayT[1] = 255;
arrayT[2] = 255;
arrayT[3] = 0;

and so on till arrayT[16348]=0;

So, if I try to print it its not printing. What I saw was these elements are integers. I tried putting characters like 'a','b' etc or '1','2' etc and the do get converted into string and when I print them I get the correct result. But I don't understand what's wrong with integer data.

Actually, I am reading an image with grey scale pixel values and storing it in an array. I want to print 25 elements at once as a string.

Thank you so much and please let me know if you have a solution for that.

Hi writetonshrama

I have elements in the array. I did mention it in my post but did not include the code for that.

for(i=0; i<resolution; i++)
{
stimArray[i] = 255.0 * abs((stimArray[i]-min)/(max-min));
}

The actual array is stimArray which contains either 0 or 255 and I want to convert that into string.

Please help me if you get a solution for that .

Thank you
1
2
3
4
5
char *arrayT;
arrayT = new char[100];

string str(arrayT);
cout << str;


you pasted this code, how can this print anything.. its empty!!

1
2
3
char arrayT[] = {'a','b','c','\0'};
string str(arrayT);
cout<< str;


this will print abc, thats correct..
if this is not your correct code, paste your correct code..
I have elements in the array. I did mention it in my post but did not include the code for that.

There is no way that anyone can really help you if you do not post a complete example of what you are trying to do. We'd love to help you but we are not clairvoyant. Well, most of us aren't. I guess I can't speak for everyone. The problem is that people are guessing as to what you might be doing and posting responses that may or may not help.

Consider this that you posted. What is the type of the array in this case? The first element is 0 so if you are trying to build a string with this you will get an empty string because the first character (assuming that this is a character array, is NULL).

1
2
3
4
arrayT[0] = 0;
arrayT[1] = 255;
arrayT[2] = 255;
arrayT[3] = 0;


Anyway, you need to learn how to use stringstreams, I think. You need to be converting numbers to characters (0 to "0" and 255 to "255"). Constructing a std::string with a char array is not doing any conversion at all. It is simply building a string class that is initialized with whatever was in the array. That isn't converting anything.
Hello Guys,

I am sorry for not pasting the original code. Here is the complete code and the code that is highlighted has got some problem. It doesn't print as a string but when I try to print individual elements then its prints the elements of the array. The elements of the array are pixel values.
Any advices are appreciated.



int main() {
int filterSize = 128;
int orientNum = 4;
int scaleStart = 3; // edited
int scaleFinish = 4; // edited
int scaleStep = 1;
int scaleNum = 1;
int imageSegments = 4;
int width=320;
int height=240;
int imgSize;
int bytesRead;
int numImages = 0;
unsigned char *imgData;
list<list<unsigned char*> > *gabors;
IplImage *gabImg, *gBuffer, *cur, *gPrev, *gCur, *diff, *binaryImg, *rotatedImg, *flippedImage, *extractedPortion;

// Sridhar added <start>
unsigned int count = 1;
char filename[100];
unsigned char *stimArray;
unsigned char *tmpImg;
unsigned char *sendStimulus;
char *loadStimulus;
int i, j;
int sort;
int resolution;
int min, max;
int sectionSize = 25;
int stimCount;
int stimArraySize;
// Sridhar added <end>

V4LStreamer *cam = new V4LStreamer(IO_METHOD_MMAP, "/dev/video0", true, width, height, 1, 4, V4L2_PIX_FMT_YUYV, V4L2_FIELD_INTERLACED, V4L2_STD_NTSC_M);

GaborFilter *gab = new GaborFilter(filterSize, orientNum, scaleStart, scaleFinish, scaleStep, scaleNum, imageSegments);

cur = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
gBuffer = cvCreateImage(cvSize(height, width), IPL_DEPTH_8U, 1);
gPrev = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
gCur = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
diff = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
gabImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);

// Sridhar added <start>
flippedImage = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
extractedPortion = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
binaryImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
rotatedImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
resolution = gabImg->imageSize;
stimCount = abs(resolution/sectionSize) + 1;
stimArraySize = stimCount * sectionSize;
stimArray = new unsigned char[resolution];
tmpImg = new unsigned char[resolution];
loadStimulus = new char[stimArraySize];
sendStimulus = new unsigned char[sectionSize];
// Sridhar added <end>

imgSize = gPrev->imageSize;
imgData = new unsigned char[imgSize];
cvNamedWindow( "OpenCV Capture", CV_WINDOW_AUTOSIZE );
cam->startCapture();

//Sridhar added <start>
for(i = 0; i < resolution; i++)
stimArray[i] = 0;

for(i = 0; i < sectionSize; i++)
sendStimulus[i] = 0;

for(i = 0; i < stimArraySize; i++)
loadStimulus[i] = 0;
//Sridhar added <end>

for ( ; ; ) {
cam->readFrame(cur->imageData, bytesRead);
cvCvtColor(cur, gCur, CV_BGR2GRAY);
//gAbsDiff(gPrev, gCur, diff, width, height, width); // Used for sequence of frames
memcpy(diff->imageData, gCur->imageData, imgSize);
gFlip90(diff, gBuffer, width, height, height, width);

memcpy(imgData, gBuffer->imageData, imgSize);

gabors = gab->filter(imgData, gBuffer->height, gBuffer->width);
memcpy(gabImg->imageData, gabors->front().front(), filterSize*filterSize);

cvShowImage( "OpenCV Capture", gabImg);

// Sridhar added <start>

// Saving current frame grabbed by the camera
sprintf(filename,"./images/current_image_%d.png",count);
// cvSaveImage(filename, cur);

// Flip original image horizontally
flipOriginalImage(gCur, flippedImage, width, height, count);

// Extract central portion of the flipped image
extractCentralPortion(flippedImage, extractedPortion, filterSize, filterSize, width, height, count);

// Saving gabor filter image
sprintf(filename,"./images/gabor_image_%d.png",count);
// cvSaveImage(filename, gabImg);

//processColorImage(cur);

memcpy(stimArray, gabImg->imageData, resolution); // Array that needs to be modified
memcpy(tmpImg, stimArray, resolution); // Array that is used to sort the data and get maximum and minimum values of the array

// Sorting data in ascending order
for(i=0; i<resolution; i++)
{
for(j=i+1; j<resolution; j++)
{
if(tmpImg[i] > tmpImg[j])
{
sort = tmpImg[i];
tmpImg[i] = tmpImg[j];
tmpImg[j] = sort;
}
}
}
min = tmpImg[0];
max = tmpImg[resolution-1];

// Applying scaling factor to generate a binary image
for(i=0; i<resolution; i++)
{
stimArray[i] = 255.0 * abs((stimArray[i]-min)/(max-min));
}

// Transferring data to IplImage (binaryImg)
memcpy(binaryImg->imageData, stimArray, resolution);

// Saving binary result of the gabor filter image
sprintf(filename,"./images/binary_image_%d.png",count);
// cvSaveImage(filename, binaryImg);

// Rotate the gabor image by an angle
// rotate(gabImg, rotatedImg, count);

// Transforming data into binary format to send to NCS
for(i = 0; i < resolution; i++)
{
if(stimArray[i] == 255.0)
{
stimArray[i] = 1;
}
}


string s = string (stimArray);
cout << s;


// Press 'Esc' key to exit
if( (cvWaitKey(10) & 255) == 27 ) break;
cvReleaseImage(&gPrev);
gPrev = cvCloneImage(gCur);

numImages++;
printf("numImages: %d\n", numImages);

// Eliminate this if structure for capturing sequence of images
if(count == 1)
{
break;
}

count++;

// Sridhar added <end>

delete gabors;
}

cam->stopCapture();
cvDestroyWindow( "mywindow" );
return 0;
}

Please post the code using appropriate tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
int main() {
int filterSize = 128;
int orientNum = 4;
int scaleStart = 3; // edited
int scaleFinish = 4; // edited
int scaleStep = 1;
int scaleNum = 1;
int imageSegments = 4;
int width=320;
int height=240;
int imgSize;
int bytesRead;
int numImages = 0;
unsigned char *imgData;
list<list<unsigned char*> > *gabors;
IplImage *gabImg, *gBuffer, *cur, *gPrev, *gCur, *diff, *binaryImg, *rotatedImg, *flippedImage, *extractedPortion;

// Sridhar added <start>
unsigned int count = 1;
char filename[100];
unsigned char *stimArray;
unsigned char *tmpImg;
unsigned char *sendStimulus;
char *loadStimulus;
int i, j;
int sort;
int resolution;
int min, max;
int sectionSize = 25;
int stimCount;
int stimArraySize;
// Sridhar added <end>

V4LStreamer *cam = new V4LStreamer(IO_METHOD_MMAP, "/dev/video0", true, width, height, 1, 4, V4L2_PIX_FMT_YUYV, V4L2_FIELD_INTERLACED, V4L2_STD_NTSC_M);

GaborFilter *gab = new GaborFilter(filterSize, orientNum, scaleStart, scaleFinish, scaleStep, scaleNum, imageSegments);

cur = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
gBuffer = cvCreateImage(cvSize(height, width), IPL_DEPTH_8U, 1);
gPrev = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
gCur = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
diff = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
gabImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);

// Sridhar added <start>
flippedImage = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
extractedPortion = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
binaryImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
rotatedImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
resolution = gabImg->imageSize;
stimCount = abs(resolution/sectionSize) + 1;
stimArraySize = stimCount * sectionSize;
stimArray = new unsigned char[resolution];
tmpImg = new unsigned char[resolution];
loadStimulus = new char[stimArraySize];
sendStimulus = new unsigned char[sectionSize];
// Sridhar added <end>

imgSize = gPrev->imageSize;
imgData = new unsigned char[imgSize];
cvNamedWindow( "OpenCV Capture", CV_WINDOW_AUTOSIZE );
cam->startCapture();

//Sridhar added <start>
for(i = 0; i < resolution; i++)
stimArray[i] = 0;

for(i = 0; i < sectionSize; i++)
sendStimulus[i] = 0;

for(i = 0; i < stimArraySize; i++)
loadStimulus[i] = 0;
//Sridhar added <end>

for ( ; ; ) {
cam->readFrame(cur->imageData, bytesRead);
cvCvtColor(cur, gCur, CV_BGR2GRAY);
//gAbsDiff(gPrev, gCur, diff, width, height, width); // Used for sequence of frames
memcpy(diff->imageData, gCur->imageData, imgSize);
gFlip90(diff, gBuffer, width, height, height, width);

memcpy(imgData, gBuffer->imageData, imgSize);

gabors = gab->filter(imgData, gBuffer->height, gBuffer->width);
memcpy(gabImg->imageData, gabors->front().front(), filterSize*filterSize);

cvShowImage( "OpenCV Capture", gabImg);

// Sridhar added <start>

// Saving current frame grabbed by the camera
sprintf(filename,"./images/current_image_%d.png",count);
// cvSaveImage(filename, cur);

// Flip original image horizontally
flipOriginalImage(gCur, flippedImage, width, height, count);

// Extract central portion of the flipped image
extractCentralPortion(flippedImage, extractedPortion, filterSize, filterSize, width, height, count);

// Saving gabor filter image
sprintf(filename,"./images/gabor_image_%d.png",count);
// cvSaveImage(filename, gabImg);

//processColorImage(cur);

memcpy(stimArray, gabImg->imageData, resolution); // Array that needs to be modified
memcpy(tmpImg, stimArray, resolution); // Array that is used to sort the data and get maximum and minimum values of the array

// Sorting data in ascending order
for(i=0; i<resolution; i++)
{
for(j=i+1; j<resolution; j++)
{
if(tmpImg[i] > tmpImg[j])
{
sort = tmpImg[i];
tmpImg[i] = tmpImg[j];
tmpImg[j] = sort;
}
}
}
min = tmpImg[0];
max = tmpImg[resolution-1];

// Applying scaling factor to generate a binary image
for(i=0; i<resolution; i++)
{
stimArray[i] = 255.0 * abs((stimArray[i]-min)/(max-min));
}

// Transferring data to IplImage (binaryImg)
memcpy(binaryImg->imageData, stimArray, resolution);

// Saving binary result of the gabor filter image
sprintf(filename,"./images/binary_image_%d.png",count);
// cvSaveImage(filename, binaryImg);

// Rotate the gabor image by an angle
// rotate(gabImg, rotatedImg, count);

// Transforming data into binary format to send to NCS
for(i = 0; i < resolution; i++)
{
if(stimArray[i] == 255.0)
{
stimArray[i] = 1;
}
}


string s = string (stimArray);
cout << s;

// Press 'Esc' key to exit
if( (cvWaitKey(10) & 255) == 27 ) break;
cvReleaseImage(&gPrev);
gPrev = cvCloneImage(gCur);

numImages++;
printf("numImages: %d\n", numImages);

// Eliminate this if structure for capturing sequence of images
if(count == 1)
{
break;
}

count++;

// Sridhar added <end>

delete gabors;
}

cam->stopCapture();
cvDestroyWindow( "mywindow" );
return 0;
}


Topic archived. No new replies allowed.