Thread: How to turn matrix of floats to unsigned bytes?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    How to turn matrix of floats to unsigned bytes?

    I have a matrix of float values that I need to make a PGM file out of. I think I need to turn these into an array of unsigned chars (correct me if I'm wrong). I need to know how to go about doing this.

    I have read about fcvt, but I'm not sure if this is what I need since it splits things into separate parts. Any help is greatly appreciated.

    Thanks in advance

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a look at what fprintf() can do. I think you'll find it adequate to the task.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is is you're actually trying to do? Store the value of the float in the range allowed for an unsigned char, or write the float out in binary to file?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    What is is you're actually trying to do? Store the value of the float in the range allowed for an unsigned char, or write the float out in binary to file?


    Quzah.
    Hmmmm... good point.... I took it as the latter.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Walk along the float matrix using a char* and store the de-referenced value in a char[].

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Quote Originally Posted by quzah View Post
    What is is you're actually trying to do? Store the value of the float in the range allowed for an unsigned char, or write the float out in binary to file?


    Quzah.
    The first. I have been trying to remove rings from images created during a neural scan. I end up manipulating arrays of floats. Now that I'm done, I need to write out a matrix of unsigned bytes.

    Quote Originally Posted by itCbitC View Post
    Walk along the float matrix using a char* and store the de-referenced value in a char[].
    This sort of sounds like it might be what I need. Is there a way to going float by float and convert it to unsigned chars along the way?

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Multiply each float by 255 and store the result in the output file as a char.

    To do the above without losing *much* information, your float must be in the range [0.0f, 1.0f]
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Quote Originally Posted by Sipher View Post
    Multiply each float by 255 and store the result in the output file as a char.

    To do the above without losing *much* information, your float must be in the range [0.0f, 1.0f]
    Store each one as a char* ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by yamahabob
    Now that I'm done, I need to write out a matrix of unsigned bytes.
    What are you going to do with that matrix of unsigned char?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by yamahabob View Post
    Store each one as a char* ?
    As a char.
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Quote Originally Posted by laserlight View Post
    What are you going to do with that matrix of unsigned char?
    Adding the proper header, this can be viewed as a PGM image.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Quote Originally Posted by Sipher View Post
    As a char.
    This seems to be working! Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'typedef struct' and 'struct'
    By nawaf in forum C Programming
    Replies: 11
    Last Post: 10-24-2010, 04:57 AM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Heap corruption using zlib inflate
    By The Wazaa in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2007, 12:43 PM
  4. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  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