Hi Friends,

How can I create a number which will be construct with the array values.

e.g.

arr[0]=2
arr[1]=5
arr[2]=8
arr[3]=4

now the number will be,

int num=2584

please help me for this...

Thanx...!!!

Recommended Answers

All 4 Replies

Well you could find out the hex value of 2584 and then place the appropriate values in the correct array elements or you could extend your array to five elements and create a c-string and use the function atoi() like below.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char ch[5] = {'2','5','8','4','\0'};
  int ans = atoi(ch);
  
  fprintf(stdout, "ans->%d\n", ans);
  return 0;
}

Hello itslucky,
You can do as gerard4143 said or you an also do this.
Declare a character array. The values in the character array can be converted to integers by doing this.

char a[5]="2358";
int a;
a = ((a[0]-'\0')*1000))+((a[1]-'\0')* 100))+((a[2]-'\0')*10)+(a[3]-'\0');

Variable 'a' will have the integer 2358.

Thank you all for such a Nice Help... it really helped me... thanx alot :-)

Sorry it should have been subtracted with '0'.

char a[5]="2358";
int a;
a = ((a[0]-'0')*1000))+((a[1]-'0')* 100))+((a[2]-'0')*10)+(a[3]-'0');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.