I am new to C programming and am trying to write one my first programs. The program rather simple, but seeing as my background is really Fortran, I am having a bit of trouble making it work.

Basically, I want to convert 16 bit float to 32 bit float and vice versa. This alone is not much of an issue ( I am sure I will figure it out), but the issue becomes somewhat complicated since I also wish to convert Big Endian to Little Endian and vice versa. This is almost trivial at this point, but I was thinking I want to use one array to do all of this. There is no reason I cannot read in any type into a common array (using stdin?), then output the response.

What I cannot figure out is how to use that function in C to read in the format into a standard array (e.g. it doesnt matter what format the data was in before, it goes into a standard array). I would love a resource to help me determine the proper "adapter" array, that will take any of these data formats so I can work on doing the conversions.

I also am having trouble detemining how to convert from the original type to the new type efficiently. I can always use a series of n^2 functions to do this, but I should be able to do that more efficiently. Any input on this would be appreciated.

I am not looking for a solution by any means, just ideas and resources so I can make this work. I need to increase my proficiency in the syntax of C. Any assistance to get me going is appreciated.

Darryl

04-18-2009

MK27

Quote:

Originally Posted by emodius

I am not looking for a solution by any means

Well you're in the right place then ;)

As you point out out, the two tasks are not very complicated. It sounds like you are going for an optimization? Or just a way to streamline the task?

You can always post some code.

04-18-2009

emodius

Thanks

Thank you for your rapid response.

Yes, I can solve the problem in any language, I think (I mean once you know how to program, it is pretty much all syntax after that).

Use n^2 functions and nested conditionals to determine the proper pairing of the original and resulting file types.

The problem is, even with the four listed types, that means I need to have four inputs, and four outputs, and every combo therein. Actually I can reuse the code in some cases I am sure, but I want to know how to determine the common format for the "read-in" array, and the best way to determine the chosen original file type and the modified file, and choose the right function. Oddly, I am confident I can write the functions themselves.

I will work on some code and come back to this thread tomorrow. It is getting late here in EDT land. Thanks for reading. I appreciate your help!

People may read this code, and I dont want to do it "stupidly" which the only way I can think of to do this.

04-20-2009

KCfromNC

What format is the data in? C doesn't have a 16-bit FP type, so you're going to be doing some processing there if the input isn't in raw hex values. You'll also have to write your own display functions for a 16-bit FP type as well as for the non-native endianness, again unless you're just writing out hex.

I'd imagine you can just store each value in a 32-bit unsigned integer. You're going to be doing bitwise operations on it to convert it from 16<->32 and BE<->LE, so an unsigned int type makes sense there. If your final output is 16 bits, just ignore the extra 16 bits.

I'd also expect you to have functions like fp16to32(), fp32to16(), be32tole32(), le32tobe32() and so on (you might get away with just passing a size in to the endian-ness functions, depending on what's easier to code). Each takes one of the 32-bit unsigned ints and returns a converted 32-bit unsigned int. You can chain these together for the operations selected instead of having a dedicated function for each particular case. Add in a 16 and 32 bit print function, and you're most of the way there.

eg print16(be16tole16(fp32to16())) would convert a 32-bit value to a 16 bit one, swap from BE to LE, and print it as a 16-bit float. Or you could have a series of if statements based on the user's selection. Whichever works better for what you're doing.