0

I am trying to convert between a double, which is between 0 and 1, and an unsigned char for the processing of an image.

What I'm doing so far is I have an array of floats.

float array[length*4]

with the 4 coming from the RGBA color scheming system. I then try this:

unsigned char newArray[4*length]
for (int i=0; i<4*length; i++){
    newArray[i] = 255*array[i];
}

for (int i=0; i<10; i++){
    printf("newArray: %d", newArray[i])
}

and get the following output:

newArray: 32
newArray: 128
newArray: 131
newArray: 0
newArray: 173
newArray: 224
newArray: 56
newArray: 60
newArray: 125
newArray: 104

when it should be this:

should be: 0
should be: 75
should be: 140
should be: 255
should be: 1
should be: 79
should be: 143
should be: 255
should be: 1
should be: 84
should be: 150
should be: 255

I have tried other methods, but they don't work.

My question to you is: how do I convert between a float and an unsigned char?

EDIT: The code that generated the correct output is this:

for (i=0; i<length; i++) {
    [[data objectAtIndex:i] getRed:&red green:&green blue:&blue alpha:&alpha];
    array[i][0] = 255*red;
    array[i][1] = 255*green;
    array[i][2] = 255*blue;
    array[i][3] = 255*alpha;

}

for (i=0; i<10; i++) {
    NSLog(@"before: %f, %f, %f, %f", array[i][0],array[i][1],array[i][2],array[i][3]);
}

, where all I did was edit the output so it was

1
2
3
4

instead of 1,2,3,4.

Community
  • 1
  • 1
Scott
  • 79
  • 3
  • 7
  • It's meaningless to convert between a float and unsigned char without more information. How do you expect it to be converted? To the integer-rounded value? To an actual character? – Joe Oct 14 '12 at 21:08
  • What is the input for the output you posted? – Daniel Oct 14 '12 at 21:16
  • @Dani: The input for those values are RGB values taken from a UIColor. I have all my data in a raw array, and am trying to convert between `float` and `unsigned char` to convert between a C array and a UIImage. – Scott Oct 14 '12 at 21:21
  • @Joe: I would like them to be RGB values, which are `int`s between 0 and 255. – Scott Oct 14 '12 at 21:21
  • @scott: I mean what input numbers generated this specific output? – Daniel Oct 14 '12 at 21:45
  • @Dani: I added the code that gave me the correct outputs, save for the generation of data which was `NSArray * data = [self getRGBAsFromImage:image atX:0 andY:0 count:image.size.height*image.size.width];`, with the getRGBAs from this [Stack Overflow answer](http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics). – Scott Oct 14 '12 at 22:11

1 Answers1

0

Convert this code:

for (int i=0; i<4*length; i++){
    newArray[i] = 255*array[i];
}

to this:

for (int i=0; i<4*length; i++)
{
    float f = 255*array[i];
    if (f < 0 || f > 255)
    {
      printf("error, array[%d]=%f, 255*array[%d]=%f\n!", i, array[i], i, f);
      abort();
    }
    newArray[i] = f;
}

and it will show you where your float is not in the range from 0 to 1.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180