Thread: Converting Between Data Types

  1. #1
    Registered User
    Join Date
    Apr 2009

    Converting Between Data Types


    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.


  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Quote Originally Posted by emodius View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009


    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).

    You have correctly identified my dilemma:

    I CAN write it to do this:

    -Read in a 32 bit float
    -have the user select "Original file is 32f format" (etc.)
    -Have the user select "Convert to 16f format" (etc.)

    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.

  4. #4
    Registered User
    Join Date
    Mar 2009
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. What are abstract data types
    By bhagwat_maimt in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 10:43 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM