CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Possible Loss of Data during Double to Unsigned Char Conversion?

  1. #1
    Join Date
    Oct 2015
    Posts
    18

    Possible Loss of Data during Double to Unsigned Char Conversion?

    Hello. I am currently trying to copy an array of ‘double’ values into an array of “unsigned char”.

    I have checked my code to know that the expected values in 2 dimensional double arrays, sig_out_imageR, sig_out_imageG and sig_out_imageB are correct (values were written to a file).

    I am currently trying to get these values into a 1 dimensional array of unsigned char for later processing. However, the values are incorrect (normally down by 1). For example, instead of the value being 36, it’s 35, or instead of the value being 204, it’s 203.

    I have tried casting the double to an unsigned char in hope of preventing loss of data but this clearly hasn’t helped.

    I am using Visual Studio 2010. Would appreciate to learn how to fix this error, thank you.

    Code:
    //put RGB from 2D array contents back into a 1D array
    unsigned char* array_1D_RGB_FFT;
    array_1D_RGB_FFT = new unsigned char[height*width*bytesPerPixel];
    if (array_1D_RGB_FFT == NULL) return 0;  // return if memory not allocated 
    
    ofstream RGBdata1D;
    RGBdata1D.open("RGBdata1D.txt");
    
    int offset = 0;
    int counter = 0;
    int bytesPerPixel = 3;
    	
    for (int j = 0; j<height; j++)  // traverse height (or rows) 
    {
        offset = width * bytesPerPixel * j;
        for (int i = 0; i<width*bytesPerPixel; i+= bytesPerPixel) // width  
       {
           array_1D_RGB_FFT[offset + i + 0] = static_cast<unsigned char>(sig_out_imageB[j][counter]);
           array_1D_RGB_FFT[offset + i + 1] = static_cast<unsigned char>(sig_out_imageG[j][counter]);
           array_1D_RGB_FFT[offset + i + 2] = static_cast<unsigned char>(sig_out_imageR[j][counter]);
    
           RGBdata1D << "B: " <<(int)array_1D_RGB_FFT[offset + i + 0]
    	            << " G: " <<(int)array_1D_RGB_FFT[offset + i + 1]
    	             << " R: " <<(int)array_1D_RGB_FFT[offset + i + 2]<< endl;
    
             ++counter;
    
       }
    	
     counter = 0;
    
    }
    
    RGBdata1D << "count of pixels: " <<c<< endl;
    RGBdata1D.close();
    Last edited by DavyS; October 16th, 2015 at 07:38 AM.

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Possible Loss of Data during Double to Unsigned Char Conversion?

    Hi!

    I suspect you already know that "unsigned char" can hold an integer value in the range 0 to 255.

    It is possible that your "double" values for some reason are slightly off, e.g. 36.99999999999999999 instead of 37, and therefore they will be rounded downwards to nearest integer.

    You can remedy the problem using

    double doublevalue = 36.99999999999999999;
    unsigned char val = (unsigned char)(doublevalue + 0.5);

    either that, or use the "round" function available in <math.h>.

    Regards
    Nobody cares how it works as long as it works

  3. #3
    Join Date
    Oct 2015
    Posts
    18

    Smile Re: Possible Loss of Data during Double to Unsigned Char Conversion?

    Quote Originally Posted by zerver View Post
    Hi!

    I suspect you already know that "unsigned char" can hold an integer value in the range 0 to 255.

    It is possible that your "double" values for some reason are slightly off, e.g. 36.99999999999999999 instead of 37, and therefore they will be rounded downwards to nearest integer.

    You can remedy the problem using

    double doublevalue = 36.99999999999999999;
    unsigned char val = (unsigned char)(doublevalue + 0.5);

    either that, or use the "round" function available in <math.h>.

    Regards
    Thank you. My program is working now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)