Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] how to convert Array[10] into Long in MikroC Pro

Status
Not open for further replies.

buddhikaneel

Member level 1
Joined
Jul 27, 2011
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,542
Hi.
int xArray[10];
long xNo;

I want to convert xarray into xNo. How can i do it
xArray[0]=1;
xArray[1]=2;
xArray[2]=3;
xArray[3]=4;
xArray[4]=5;
xArray[5]=6;
xArray[6]=7;
xArray[7]=8;
xArray[8]=9;
xArray[9]=0;

result could be

xNo=1234567890;

____________________________________
Can you tell me is there another way to do this.
I will reading Keypad Keypress into that array. Finally i want to 10 Digit long data from which is last pressed 10 keypad strocks.
what can i do..?
 

is the content of the array 1,2,3,4,5.... or '1','2','3'... because in the second case the ASCII characters actually represent 0x31.0x32,0x33...

Alex
 
the simplest way I can think of is

unsigned long xNO;

xNO=xArray[0]*1000000000+xArray[1]*100000000+xArray[2]*10000000+xArray[3]*1000000+xArray[4]*100000+xArray[5]*10000+xArray[6]*1000+xArray[7]*100+xArray[8]*10+xArray[9];
 
I also think about that. But i don't know there are any functions in mikroc pro to do that. how ever i'll use this method for now.

Thanks for help..
 

actually there is a function named atol, it is included in stdlib.h, check if your IDE has it but note that you have to include the library which will take more space that using the custom function of the previous post.

**broken link removed**

Alex

---------- Post added at 16:28 ---------- Previous post was at 16:15 ----------

Just a note, atol works by converting the characters '1','2','3' so you have to convert 1,2,3 to 0x31,0x32,0x33... you can easily do it using (value+0x30) or you can assign directly 0x30 to 0x39 instead of 0x0 to 0x9 when the keyboard button is pressed.
 
hi

what if its a string?
like this
str1[]='a';
str2[]='b';
str3[]='c';
str4[]='d';
str5[]='e';
str6[]='f';
str7[]='g';
str8[]='h';
str9[]='i';
str10[]='j';

result could be

message[]="abcdefghij";
 
Last edited:

copy the letters one by one to the message string
 
  • Like
Reactions: tonete

    tonete

    Points: 2
    Helpful Answer Positive Rating
copy the letters one by one to the message string

that does mean a is index 0, b is in index 1, c is in index 2, and so on...?

or i will hav to use strcpy?

sorry..im not expert
 

Re: how to convert strings with one letter into one word

You can use string concatenation like
message = str1 + str2 + str3 + str0;
 
  • Like
Reactions: tonete

    tonete

    Points: 2
    Helpful Answer Positive Rating
Re: how to convert strings with one letter into one word

by the way...its for my mikroc code
 

The simplest way is
Code:
message[0]=str1[0];
message[1]=str2[0];
message[2]=str3[0];
message[3]=str4[0];
message[4]=str5[0];
message[5]=str6[0];
message[6]=str7[0];
message[7]=str8[0];
message[8]=str9[0];
message[9]=str10[0];

I'm not sure why you have specified arrays to hold one charter in each one, why not simple variables?
 

Re: how to convert strings with one letter into one word

individual array for single character storage is not an efficient way...


this may help you. i am not sure ! :-?


Code:
unsigned char *ptr[10];
ptr[1]=str1[0];
      .
      .
      .
ptr[9]=str10[0];



i=0;
while(i<9)
message[i]=*ptr[i++];
 
  • Like
Reactions: tonete

    tonete

    Points: 2
    Helpful Answer Positive Rating
Re: how to convert strings with one letter into one word

You can use string concatenation like
message = str1 + str2 + str3 + str0;

This is embedded C , there are no strings like the ones you describe, these are arrays and the concatenation will not work

- - - Updated - - -

individual array for single character storage is not an efficient way...


this may help you. i am not sure ! :-?


Code:
unsigned char *ptr[10];
ptr[1]=str1[0];
      .
      .
      .
ptr[9]=str10[0];



i=0;
while(i<9)
message[i]=*ptr[i++];

The first member of the array is ptr[0], not ptr[1]

To assign the address of an array member to a pointer you have to do one of the following

Code:
ptr[0]=[COLOR="#FF0000"]&[/COLOR]str1[0];
ptr[0]=str1; // the same as &str1[0]

Alex
 

ptr[1] is my mistake ptr[0] is correct.
 
Last edited:
  • Like
Reactions: tonete

    tonete

    Points: 2
    Helpful Answer Positive Rating
&str1[0] is the address of member 0
str1[0] is the content of member 0

these are not equal or interchangeable
 

sorry for the inconvenience

your are right.
&str1[0] = str1 = &str1
 
  • Like
Reactions: tonete

    tonete

    Points: 2
    Helpful Answer Positive Rating
if its something like this?

Code:
unsigned char getKeyboard(){
do {
if (Ps2_Key_Read(&keydata, &special, &down)) { 
if (down && !special && keydata) {
   if(keydata==97){kb=97;}  //a
   if(keydata==98){kb=98;}  //b
   if(keydata==99){kb=99;}  //c
   if(keydata==100){kb=100;} //d
   if(keydata==101){kb=101;} //e
   if(keydata==102){kb=102;} //f
   if(keydata==103){kb=103;} //g
   if(keydata==104){kb=104;} //h
   if(keydata==105){kb=105;} //i
   if(keydata==106){kb=106;} //j
   if(keydata==107){kb=107;} //k
   if(keydata==108){kb=108;} //l
   if(keydata==109){kb=109;} //m
   if(keydata==110){kb=110;} //n
   if(keydata==111){kb=111;} //o
   if(keydata==112){kb=112;} //p
   if(keydata==113){kb=113;} //q
   if(keydata==114){kb=114;} //r
   if(keydata==115){kb=115;} //s
   if(keydata==116){kb=116;} //t
   if(keydata==117){kb=117;} //u
   if(keydata==118){kb=118;} //v
   if(keydata==119){kb=119;} //w
   if(keydata==120){kb=120;} //x
   if(keydata==121){kb=121;} //y
   if(keydata==122){kb=122;} //z

   } } Delay_ms(5);
return kb;

} while (1);

}

interfacing keyboard...

when i press the a key in the keyboard..the value is being return in kb..the next press will be added to the ist one and so on
after i press enter..that will be the time it displays the word in the lcd

Code:
if (down && (keydata==13)){ break;}     // enter


example
first press ---- h
2nd ---- e
3rd ---- l
4th ---- l
5th ---- o


result would be "hello"

thank you

(mikroC for PIC is used for coding)
 
Last edited:

You may try this:
Code:
#define MAX 100	// maximum number of letters the array will hold

int main()
{
	unsigned char letter;	// uchar to hold the current letter
	unsigned char letterArray[MAX];	// array to hold the string
	int index= 0;	// start with 0 index


	while(1) {
		letter= getKeyboard();	// get letter

		if(letter!= 13)
			letterArray[index++]= letter;	// insert next char
		else {
			letterArray[index]= '\0';	// terminate current string
			// lcd printing code here to print 'letterArray'
			// ...
			index= 0;	// reset index to start again
		}
	}
}
 
  • Like
Reactions: tonete

    tonete

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top