How to conversion array to one string...

array of ints int a single string

Page 1 of 1

7 Replies - 2022 Views - Last Post: 01 July 2007 - 08:35 AM Rate Topic: -----

#1 imamkomc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

How to conversion array to one string...

Post icon  Posted 10 June 2007 - 01:41 AM


imamkomc@gmail.com

Help me,
I have problem about method to convert array (type int) to one string (type char),
e.g. input as :
array[1]=1
array[2]=2
array[3]=3
array[4]=4
.
.
.
array[n]=n
stuff of every array are independent.

If i want in the output denote an one string as below :
I am defined variabel name of string is *string than the product is string="1234.....n".

How to implementation solve the problem in the C++ Program.

Thanks.
ImamkomC++



Is This A Good Question/Topic? 0
  • +

Replies To: How to conversion array to one string...

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: How to conversion array to one string...

Posted 10 June 2007 - 02:56 AM

View Postimamkomc, on 10 Jun, 2007 - 01:41 AM, said:


imamkomc@gmail.com

Help me,
I have problem about method to convert array (type int) to one string (type char),
e.g. input as :
array[1]=1
array[2]=2
array[3]=3
array[4]=4
.
.
.
array[n]=n
stuff of every array are independent.

If i want in the output denote an one string as below :
I am defined variabel name of string is *string than the product is string="1234.....n".

How to implementation solve the problem in the C++ Program.

Thanks.
ImamkomC++



hi

Conversion falls into two categories:
  • Implicit
  • Explicit
Implicit occurs when the compiler will allow the conversion and does so at compile time. An integer and a char can be converted implicitly by this code:
int a = 5;
char b = a;


Explicit occurs when the programmer uses one of three mechanisms in code that can force conversion. These are
referred to as typecasting, and there are three that I can think of:
  • (new_type) old_type
  • static_cast < new_type > old_type
  • dynamic_cast < new_type > old_type

Each one has uses throughout code, it is preferred to use static_cast in lieu of (new_type) old_type. dynamic_cast is generally used in the OOP arena to enable casting up and down the inheritance tree.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to conversion array to one string...

Posted 10 June 2007 - 10:31 AM

You know Gregory I like to read you occasional posts because I learn a lot. Not really the broad topics but the little things that I have either overlooked or forgotten, or never really understood. I myself have never really known what the difference or reason behind having a static_cast and a dynamic_cast. I just use the standard C cast unless (for example windows programming) it just seems to be what everyone else is doing (i.e. I copied code from somewhere and never changed that line because I didn't really know what it did.)

Though I am nor sure that discussion really helps change an array of integers into a string... Does the string class have a native conversion from integers to strings?

I would think that a loop using a stringstream or an sptrintf() would be the way to go.
Was This Post Helpful? 0
  • +
  • -

#4 RautRupali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 07-June 07

Re: How to conversion array to one string...

Posted 12 June 2007 - 01:46 AM

View PostgregoryH, on 10 Jun, 2007 - 02:56 AM, said:

View Postimamkomc, on 10 Jun, 2007 - 01:41 AM, said:


imamkomc@gmail.com

Help me,
I have problem about method to convert array (type int) to one string (type char),
e.g. input as :
array[1]=1
array[2]=2
array[3]=3
array[4]=4
.
.
.
array[n]=n
stuff of every array are independent.

If i want in the output denote an one string as below :
I am defined variabel name of string is *string than the product is string="1234.....n".

How to implementation solve the problem in the C++ Program.

Thanks.
ImamkomC++



hi

Conversion falls into two categories:
  • Implicit
  • Explicit
Implicit occurs when the compiler will allow the conversion and does so at compile time. An integer and a char can be converted implicitly by this code:
int a = 5;
char b = a;


Explicit occurs when the programmer uses one of three mechanisms in code that can force conversion. These are
referred to as typecasting, and there are three that I can think of:
  • (new_type) old_type
  • static_cast < new_type > old_type
  • dynamic_cast < new_type > old_type
Each one has uses throughout code, it is preferred to use static_cast in lieu of (new_type) old_type. dynamic_cast is generally used in the OOP arena to enable casting up and down the inheritance tree.

Hope this helps.



Hi,
I tried to use static_cast for conversion but while displaying using cout it prints garbage value.

I tried to convert it using itoa() function...

Is it the correct way.. please tell
# include<iostream.h>
# include<stdlib.h>
# include<stdio.h>

int main()
{
	int iarr[4],i;

	char sarr[8][8];

	for (i=0;i<3;i++)
	{
		cin>>iarr[i];
	}
	for (i=0;i<3;i++)
	{
	_itoa(iarr[i],reinterpret_cast<char *>(sarr[i]),10);

	}

	 for (i=0;i<3;i++)
	{

	cout<<reinterpret_cast<char *>(sarr[i]);
	}

 //i= sprintf( buffer,	 "   String:	%s\n", istr[0] );

//printf( "Output:\n%s\ncharacter count = %d\n", buffer, i );

 //j= sprintf( buffer,	 "   String:	%s\n", istr[1] );

//printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
	 	

	return 0;
}


EDIT : Code Tags Added
MOD COMMENT : Enclose Source Code within the [ code ] and [ /code ] block.
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: How to conversion array to one string...

Posted 12 June 2007 - 02:59 AM

For smaller values of n, you can use a single loop and extract the number into an unsigned long variable, and then use the itoa() function.

Eg. With reference to your example:
1234 = (4 * 10^0) + (3 * 10^1) + (2 * 10^2) + (1 * 10^3)

^ = Power Sign.

This will work only for numbers that fall below the unsigned long range.
Was This Post Helpful? 0
  • +
  • -

#6 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: How to conversion array to one string...

Posted 12 June 2007 - 06:53 AM

You can convert numbers to strings using the sprintf() function.
char *str;
int a = 5;

sprintf(str, "%d", a);



sprintf() does not automatically expand the string, so make sure you allocate enough space.
Was This Post Helpful? 0
  • +
  • -

#7 imamkomc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

Re: How to conversion array to one string...

Posted 22 June 2007 - 02:03 AM

Hi all,
I try to make a program, but output is bad.

this my program (convert few arrays to single string) :
#include <stdio>
#include <fstream>
#include <stdlib>
#include <conio>
#include <iomanip>
#define bit 1000 //can be change
void main()
{
int i,*arr,length,j,*split_str; 
arr=new int[bit];
split_str=new int[bit];
char *str="",temp[1]; // init str_init
for(i=1;i<=10;i++){
arr[i]=random(10);
cout<<"arr["<<i<<"]="<<arr[i]<<"\n";
//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
}
cout<<"\n\n";
for(j=1;j<=10;j++){
itoa(arr[j],temp,10);
//cout<<"\ntemp ="<<temp<<"\n";
str=strcat(str,temp);
//cout<<"\nstr["<<j<<"]="<<str<<"\n";
}
cout<<"\nresult = "<<str<<"\n";

//order of strlen(str)
length=strlen(str);
cout<<"\nlength digit ="<<length<<"\n";
//spit string
cout<<"\n";
for(i=1;i<=length;i++){
split_str[i]=str[i-1]-48;
cout<<"split_str["<<i<<"]="<<split_str[i]<<"\n";
//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
}
getch();
}



In order "for(i=1;i<=10;i++){", if i change value 10 become 100 or greater (>100), than output of program is bad.

help me.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 imamkomc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 64
  • Joined: 09-May 07

Re: How to conversion array to one string...

Posted 01 July 2007 - 08:35 AM

Thank to all which many help me,
So, i can make whole program to be good.

#include <stdio>
#include <fstream>
#include <stdlib>
#include <conio>
#include <iomanip>
#define bit 1000
void main()
{
int i,*arr,length,j,*split_str;
arr=new int[bit];
split_str=new int[bit];
char *str,*temp;
str=new char[bit];
temp=new char[1];
	for(i=1;i<=100;i++){
		arr[i]=random(10);
		cout<<"arr["<<i<<"]="<<arr[i]<<"\n";
		//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
	}
cout<<"\n\n";
	str=strcpy(str,"");// str initial must be value NULL
	for(j=1;j<=100;j++){
		itoa(arr[j],temp,10); //10 explain type number (biner(2),decimal(10),.)
		//cout<<"\ntemp ="<<temp<<"\n";
		str=strcat(str,temp);
		//cout<<"\nstr["<<j<<"]="<<str<<"\n";
	}
cout<<"\nresult = "<<str<<"\n";
getch ();
//order strlen(str)
length=strlen(str);
cout<<"\nlength number ="<<length<<"\n";
//spit string
cout<<"\n";
cout<<"\nsplit again :\n";
	for(i=1;i<=length;i++){
		split_str[i]=str[i-1]-48;
		cout<<"split_str["<<i<<"]="<<split_str[i]<<"\n";
		//cout<<"char_arr["<<i<<"]="<<itoa(temp,)<<"\n";
	}
getch();
delete [] arr;
delete [] split_str;
delete [] str;
}



Regard,
ImamKomC++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1