Results 1 to 10 of 10

Thread: Converting a numerical char array to a long?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    Dallas,TX
    Posts
    170

    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

  2. #2
    Junior Member
    Join Date
    Jul 2003
    Posts
    18
    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.

  3. #3
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Is there a reason you declare a 4 element array but only use 3?

  4. #4
    Junior Member
    Join Date
    Jul 2003
    Posts
    18
    unsigned char x[3];
    declares a three element array, right?

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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

  6. #6
    Junior Member
    Join Date
    Jul 2003
    Posts
    18
    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.

  7. #7
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh God, NOOOOO!

    type name[size];
    Declares an array with size elements, not size+1. No arguing.

    This will output 1234
    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.

  9. #9
    Junior Member
    Join Date
    Jul 2003
    Posts
    18
    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!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    Dallas,TX
    Posts
    170
    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
  •  



Click Here to Expand Forum to Full Width