C function to return byte array from signed short array in linux

I need an example of a function to take a signed sort array and return a byte array written in C for Linux.  I'm new to C and need and need an explicit example please.
StarDusterIIAsked:
Who is Participating?
 
Infinity08Commented:
Depending on the purpose, you can simply cast a pointer to signed short to a pointer to unsigned char :

        short array[SIZE];
        unsigned char* bytes = (unsigned char*) array;

It depends on what this will be used for though. Maybe you can give a bit more background information about what this is for ?
0
 
StarDusterIIAuthor Commented:
It's for an audio routine.  Input to the audio conversion routine needs a byte array and the output from the input source is a short array.
0
 
Infinity08Commented:
Ok, and what does the documentation say about the format of both arrays ?
0
 
StarDusterIIAuthor Commented:
Documentation??  How funny!!   ;-)

Your suggestion actually worked but I have an endian problem.  Anyway to change  the order of bytes in the short array so they come out reversed?
0
 
Infinity08Commented:
>> Your suggestion actually worked but I have an endian problem.

Which is what the documentation should have showed : the expected byte order.

If there's no (proper) documentation, then the API you're using is more difficult to use correctly (and might involve trial and error).


>> Anyway to change  the order of bytes in the short array so they come out reversed?

Assuming that a short on your system is 2 bytes, you could swap around every pair of bytes in the byte array.
Or you could use htons for that :

        http://linux.die.net/man/3/htons
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.