|
-
Aug 15th, 2003, 09:08 PM
#1
Thread Starter
Addicted Member
Converting a numerical char array to a long?
I'm working with some C code on an embedded processor (8051)in which I have an array:
Code:
unsigned char x[3]; // unsigned char is a 8-bit value on 8051
x[0] = 0x02;
x[1] = 0xFF;
x[2] = 0xFF;
I then have an unsigned long variable in which I would like to equal 0x02FFFF (in the example above). On the 8051, the unsigned long variable is 32-bits.
Code:
unsigned long i;
i = ???
So how would this be accomplished?
Thanks in Advance
Philip
-
Aug 16th, 2003, 04:14 PM
#2
Junior Member
How about:
Code:
unsigned char x[3]; // unsigned char is a 8-bit value on 8051
x[0] = 0x02;
x[1] = 0xFF;
x[2] = 0xFF;
unsigned long i = 0;
for (int index = 0; index < 3; index++)
{
i <<= 8;
i |= x[index];
}
// or
unsigned long i = x[0];
i = i << 8;
i = i | x[1];
i = i << 8;
i = i | x[2];
Last edited by jlou; Aug 16th, 2003 at 05:33 PM.
-
Aug 16th, 2003, 05:11 PM
#3
Is there a reason you declare a 4 element array but only use 3?
-
Aug 16th, 2003, 05:32 PM
#4
Junior Member
unsigned char x[3];
declares a three element array, right?
-
Aug 16th, 2003, 07:10 PM
#5
Originally posted by jlou
unsigned char x[3];
declares a three element array, right?
No, [3] is a 4 element array.
0 is the first number
-
Aug 16th, 2003, 08:54 PM
#6
Junior Member
I think when you are declaring an array, the number inside is the size, in this example, 3.
Then when you are accessing the array, the number inside is the index, starting with 0. Indexes 0, 1, and 2 are valid in an array of size 3.
So:
int myArray[3]; // declares an array of size 3.
int x = myArray[2]; // set x to the 3rd element in myArray.
and:
int myArray[4]; // declares an array of size 4.
int x = myArray[3]; // set x to the 4th element in myArray.
-
Aug 16th, 2003, 09:36 PM
#7
no
Code:
#include <iostream>
using namespace std;
int main()
{
int myarray[3];
myarray[0] = 1;
myarray[1] = 2;
myarray[2] = 3;
myarray[3] = 4;
cout << myarray[0] << myarray[1] << myarray[2] << myarray[3] << endl;
}
This will output 1234
myarray[3] holds 4 elements.
I would also like to recommend the use of vectors, arrays suck compared to vectors
Last edited by Kasracer; Aug 16th, 2003 at 09:46 PM.
-
Aug 18th, 2003, 04:35 AM
#8
Oh God, NOOOOO!
type name[size];
Declares an array with size elements, not size+1. No arguing.
Only that this means nothing. Try this code:
Code:
#include <iostream>
using namespace std;
int main()
{
int previous;
int myarray[3];
int next;
myarray[0] = 1;
myarray[1] = 2;
myarray[2] = 3;
myarray[3] = 4;
previous = 0;
next = 0;
cout << myarray[0] << myarray[1] << myarray[2] << myarray[3] << endl;
}
Now try again. As you can see, with myarray[3] you are accessing memory that does not belong to the array!
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Aug 18th, 2003, 12:50 PM
#9
Junior Member
Originally posted by kasracer
I would also like to recommend the use of vectors, arrays suck compared to vectors
Now there is something I very much agree with!
-
Aug 18th, 2003, 12:51 PM
#10
Thread Starter
Addicted Member
In other words, yes...it's only a 3 element array..hehe.
Thanks for the idea jlou. Both the shifting will do the job or casting a pointer. I could also use a union to access the individual bytes, but I had too many variables to redefine.
Thanks
P
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|