Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-25-2008, 03:25 PM   #1
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
unsigned long int uint32_t to unsigned char and back

I have an application in which there is an array of ID's as uint32_t. I have to pass this data through a couple functions which have a struct of unsigned char available to pass through the data.

I'm worried about how to properly do this so that it doesn't cause any problems on other platforms. The data is ONLY going to be manipulated as uint32_t*... it's just being passed around as unsigned char*.

typedef struct
  unsigned char *data;
  int length;
} SomeData;

void some_function_requiring_struct (SomeData *data)
  uint32_t *numbers = (uint32_t*)data->data;

  /* it's safe to manipulate numbers now... right? */

/* say we're in some function */

int len;
uint32_t *numbers;
len = some_function_populating_uint32_data (&numbers);

/* allocate struct */

data->data = (unsigned char*)data;
data->len = len;

some_function_requiring_struct (data);
Is this okay?
Old 05-26-2008, 08:08 AM   #2
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
You're going to have endianness troubles by this approach as the byteorder can differ between platforms, and the conversion from the byte array to the integer array could fail.

There are functions like e.g. the ntoh*/hton* family

Under the hood they just do a simple byteswapping if necessary, or nothing at all if no conversion is needed.

Last edited by fantas; 05-26-2008 at 08:14 AM. Reason: added link
Old 08-02-2009, 01:39 AM   #3
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
The high-endian/low-endian will only be an issue if the data is being transferred to another machine. So for example, if any network code is involved, or permanent data files, then yes, you do need to serialize the data rather than just use casting.

Arrays are packed in C, so you shouldn't have any packing issues. This would become an issue if you were using structs for the data.

You can sometimes get alignment issues when converting pointers (though not in your example), particularly when casting pointers to char.

If you really have to do something like this, I'd suggest using void pointers rather than char pointers so that it is clear that the data type has been lost.

And if there is any possibility of using C++ (even if it is just a subset), there are nicer ways to do this that don't involve casting. Using C++ doesn't mean changing all the code to an object oriented model.

Code like your example indicates one of several things: (1) use of legacy library functions which cannot be changed (so they need to have a wrapper around them); (2) data which is going to be serialized (so it should be done in a platform independent way); or (3) trying to achieve object inheritance in a non-object based language (use pointers to implement inheritance, or move to C++).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid conversion from 'long unsigned int*' to 'uint32*' quarry_06 Programming 8 12-23-2005 11:00 AM
How to print unsigned long int alaios Programming 6 07-14-2005 10:27 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
64 bit CPU unsigned long int GodSendDeath Programming 8 03-29-2005 01:19 PM
int value larger then unsigned long jpc82 Programming 3 03-12-2004 03:54 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration