Thread: float to unsigned char

  1. #1
    Registered User
    Join Date
    Aug 2005

    float to unsigned char

    Please observe the following code:

    *(image->data + j + (i * image->width)) = (1/sqrt(2.0)) * sum;
    *(image->data + j + (i * image->width))
    This left hand side points to a memory space of type 'unsigned char'.

    sum is a variable of type float.

    My issue is I need the left hand side to store the number from the right hand side in the 'unsigned char' data space.

    This is because the right handside values are in the range 0-255. Therefore I do NOT need the extra space that int offers.

    But the problem is the left handside just inteprets the number from the right hand side as an ASCII code. This I don't want to happen.

    I want to store actual number i.e. if I get 255, then store 255 on the left hand side.

    I've tried casting the right handside, but no luck.

    I'm sure I'm missing some fundamental point here.
    Disk space: the final frontier

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Quote Originally Posted by cblix
    I'm sure I'm missing some fundamental point here.
    Probably just how you are observing the value.
    Last edited by Dave_Sinkula; 02-15-2006 at 09:19 AM. Reason: Tripped on my tongue.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    West Virginia
    An "ASCII code" is just a integer number between 0 and 255 that can be stored in a byte. If you view it as a character, you'll see a graphic (if there is one for that code), if you view it as an unsigned byte value, you'll see an integer with a value between 0 and 255. The ASCII character 'A' has a value of 65 viewed as a decimal number.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  3. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 06:47 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM