Thread: Converting unsigned long array to unsigned char array

  1. #1
    Registered User
    Join Date
    Sep 2009

    Converting unsigned long array to unsigned char array

    I'm working with an embedded system that transfers data over a 32-bit bus, so I have a variable defined as follows:

    unsigned long UserData[4];

    As you can tell, this would be 128-bits of data. After I get this data from the bus, I have a function that accepts 128-bits of data as an argument, but in the form of an unsigned character array as follows;

    unsigned char FuncData[16];

    // Both arguments are defined in the prototype as unsigned char's
    RandomFunction(FuncData, UserData);

    From looking at the above function, I want to input this UserData into the function - what would be the best way to typecast or convert this unsigned long array to an unsigned character array?

    Thanks for the help, I greatly appreciate it!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    I would type its name "UserData". (as in
    RandomFunction(FuncData, UserData);
    assuming there's no endianness issues). (If there are endianness issues, it was wrong to use an array of longs in the first place.)

  3. #3
    Registered User
    Join Date
    Jun 2008
    It kind of depends on how you want to deal with the data. If you want to change each and every byte. Then (char*)longarray[3] should do the trick. If you want to treat each char value as an overflowed value, (IE the values SHOULD be between 0 and 255) then, well, don't do that . (it can be done, but it is very hacky. you can add 4 to each indexing value to get it done, ie chararray[i + 4]; will get the job done (though, you'll have to start with -4)

    As was stated, you'll need to watch out for the endians
    Last edited by Cogman; 09-07-2009 at 09:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 08:57 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM

Tags for this Thread