0

What is the best way to go about converting a uint8 into a normal int I want to do some work with an rgb histogram, but I can't find a correct approach to the final piece here (updated to rm stbi lib)

proc obc*(image1, image2: string) =
  type IntArray = array[24,int]
  var
    r24: IntArray
  let f:float = 24/256
  var
    data1: seq[uint8]
  #data1 = stbi.load(image1,w,h,c,stbi.Default) #uint8

  for i in countUp(0, data1.len - 3, step = 4):
    let
      r1: uint8 = data1[i]
    r24[ (int)(r1*f) ] = r24[(int)(r1*f)] + 1

I thought of doing something like converting the r1 to a string and then doing parseInt(), but that just seems perverse. Is there a cleaner way?

Here is the compiler error

Error: type mismatch: got <uint8, float>
but expected one of:
proc `*`(x, y: uint): uint
  first type mismatch at position: 2
  required type for y: uint
  but expression 'f' is of type: float
proc `*`(x, y: uint16): uint16
  first type mismatch at position: 2
  required type for y: uint16
  but expression 'f' is of type: float
proc `*`(x, y: uint32): uint32
  first type mismatch at position: 2
  required type for y: uint32
  but expression 'f' is of type: float
proc `*`(x, y: uint64): uint64
  first type mismatch at position: 2
  required type for y: uint64
  but expression 'f' is of type: float
proc `*`(x, y: uint8): uint8
  first type mismatch at position: 2
  required type for y: uint8
  but expression 'f' is of type: float
8 other mismatching symbols have been suppressed; compile with --showAllMismatches:on to see them

expression: r1 * f
SlightlyKosumi
  • 701
  • 2
  • 8
  • 24
  • If you have an uint8 variabel (`var v : uint8`), I think you can convert it to an int using `(int)v`. ( Sorry I cannot test because of some virus warning when trying to install num on my Windows PC ) – Luuk Jul 03 '21 at 15:12
  • That is kind of what I tried to do in my code sample. Did I miss something else? – SlightlyKosumi Jul 03 '21 at 15:53
  • Like i said, I cannot install `nim` on my Windows PC, so I do not have a clue what "this doesn't compile" means. Does it return an error, and if so what text is in the error message? – Luuk Jul 03 '21 at 16:05
  • What is `stbi`? And `image1`? What libraries are involved here? Anyway, you should be able to transform `uint8` into `int` with `int(data1[i])`, for example. Also, the compiler output instead of "doesn't compile" would help. – xbello Jul 03 '21 at 17:45

2 Answers2

1

Your mistake was to forget that Nim is strongly typed. You tried to multiply a uint8 with a float. Either you multiply float with float or uint8 with uint8. There are exceptions, see: https://nim-lang.org/docs/manual.html#type-relations-convertible-relation

Here's an example to convert uint8 to float:

let x = 1'u8
let y = float(x)
let z = x.float

float to uint8:

let x = 1.0
let y = uint8(x)
let z = x.uint8

Example of your code that compiles, but I don't know if that's the result you want:

proc obc*(image1, image2: string) =
  type IntArray = array[24,int]
  var
    r24: IntArray
  let f:float = 24/256
  var
    data1: seq[uint8]
  #data1 = stbi.load(image1,w,h,c,stbi.Default) #uint8

  for i in countUp(0, data1.len - 3, step = 4):
    let
      r1: uint8 = data1[i]
      
    r24[int(r1 * uint8(f))] = r24[int(r1 * uint8(f))] + 1
rockcavera
  • 11
  • 1
-1

Well, it seems my code was just worse than I suspected By changing this line from

r1: uint8 = data1[i]

to

r1: float = (float)data1[i]

the compilation error is gone. I had thought that this was a problem with uint8 originally, but no. It was just a simple float int mismatch that required an additional cast.

so probably the original suggestion was right, and this kind of thing is fine

var u: uint8 = 8
var i: int = (int) u
SlightlyKosumi
  • 701
  • 2
  • 8
  • 24