warning: narrowing conversion of '-1' from 'int' to 'unsigned int' inside { } [-

Hi all,

I’ve googled this but other issues I see are all related to converting int to char. I’m just assigning integers to unsigned int.

This is an example code replicating the issue:

unsigned int rawCodes[3] = {0, 1, -1};

Any negative values drop this error:

  • warning: narrowing conversion of ‘-1’ from ‘int’ to ‘unsigned int’ inside { } [-Wnarrowing]

This is for an IR sender project I’m doing just to have a play. I read the raw valued using a reader which gives me many positive and negative values, then I send it using the IRRemote Library.

What am I missing or should change?

Here’s the full code:

#include <IRremote.h>

IRsend irsend;

unsigned int rawCodes[439] = {3528, -1696, 440, -428, 440, -1292, 468, -400, 440, -424, 440, -428, 468, -400, 440, -424, 412, -456, 440, -424, 468, -400, 464, -428, 440, -428, 440, -424, 440, -1292, 468, -400, 436, -428, 444, -424, 444, -424, 440, -424, 440, -428, 440, -424, 444, -1288, 412, -1320, 440, -1296, 468, -396, 468, -428, 436, -1296, 436, -428, 440, -428, 440, -424, 440, -428, 440, -428, 436, -428, 412, -456, 440, -424, 440, -428, 440, -424, 440, -428, 436, -432, 436, -428, 440, -452, 444, -424, 436, -428, 440, -428, 440, -424, 416, -452, 440, -428, 440, -424, 440, -428, 440, -424, 440, -428, 440, -428, 436, -428, 444, -424, 468, -424, 440, -424, 444, -424, 444, -1288, 468, -1264, 440, -428, 436, -432, 440, -424, 444, -424, 436, -428, 412, -10008, 3528, -1700, 440, -424, 444, -1292, 464, -428, 440, -428, 436, -428, 440, -424, 440, -428, 444, -424, 436, -428, 444, -424, 440, -424, 468, -400, 440, -424, 444, -1292, 436, -428, 440, -428, 468, -424, 440, -428, 468, -396, 444, -424, 468, -396, 444, -1288, 440, -1296, 436, -1292, 472, -396, 472, -396, 440, -1292, 440, -424, 444, -424, 440, -428, 436, -428, 444, -424, 464, -428, 440, -428, 440, -424, 440, -428, 440, -424, 472, -396, 440, -424, 440, -428, 440, -1292, 468, -1264, 440, -1292, 440, -1292, 472, -396, 440, -428, 440, -1292, 496, -396, 468, -400, 440, -424, 440, -1292, 440, -1296, 436, -1296, 440, -1292, 440, -424, 444, -424, 440, -424, 444, -424, 440, -428, 440, -424, 440, -428, 440, -424, 440, -428, 468, -1292, 468, -1264, 436, -428, 440, -1296, 440, -424, 440, -1292, 440, -1292, 468, -1264, 444, -424, 468, -1264, 440, -428, 436, -428, 468, -1264, 444, -424, 440, -428, 440, -424, 468, -424, 444, -424, 440, -428, 436, -428, 468, -400, 440, -424, 468, -400, 440, -424, 452, -416, 440, -428, 440, -1292, 440, -1292, 464, -1268, 440, -424, 440, -428, 468, -424, 440, -428, 440, -428, 440, -424, 440, -428, 440, -424, 440, -428, 440, -1292, 468, -1264, 440, -1292, 468, -396, 444, -424, 440, -428, 440, -424, 440, -428, 440, -452, 440, -428, 440, -424, 440, -428, 440, -424, 444, -424, 440, -428, 468, -396, 444, -424, 440, -424, 444, -424, 440, -1292, 440, -428, 440, -424, 468, -1264, 468, -428, 440, -424, 444, -420, 444, -1292, 440, -424, 468, -400, 440, -424, 444, -424, 440, -428, 440, -424, 444, -424, 468, -396, 444, -424, 440, -424, 468, -428, 468, -396, 444, -424, 440, -424, 468, -400, 444, -420, 472, -396, 468, -1264, 440, -1292, 440, -428, 468, -400, 440, -424, 468, -396, 464, -404, 472};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
    irsend.sendRaw(rawCodes, 439, 38);
    Serial.println("Sent the code");
    delay(10000);
}

Thanks

Hmmmm. That's a poser alright.

unsigned int is set of non-negative number. So you can't have negative numbers in there. You need to change the type to int.

arduino_new:
unsigned int is set of non-negative number. So you can't have negative numbers in there. You need to change the type to int.

Ah OK. I gave that a go but the function irsend.sendRaw needs unsigned int:
void IRsend::sendRaw(const unsigned int*, unsigned int, unsigned int)

boolrules:
Hmmmm. That's a poser alright.

Ey?

CheeseNWine:
Ah OK. I gave that a go but the function irsend.sendRaw needs unsigned int:
void IRsend::sendRaw(const unsigned int*, unsigned int, unsigned int)

Maybe you should explain what you think you want to do. The IR library does expect an unsigned integer and for a good reason. What are you hoping to send with the -1?

You could get the exact same effect this way BTW:

unsigned int rawCodes[3] = {0, 1, 65535};

Would that work for you?

Delta_G:
Maybe you should explain what you think you want to do. The IR library does expect an unsigned integer and for a good reason. What are you hoping to send with the -1?

Thanks Delta-G, OK I’m just trying to do an ordinary infrared read and send. I already read the values and now I’m stuck in sending the codes due to those <0 numbers. the -1 was just an example and not the actual code I’m sending. I included the full sketch in the opening post.

So if the sender only expects positive numbers (and this is a popular library), then did I do the read incorrectly to get negatives? What’s going on here!!!

No no the remote doesn't send "numbers". It sends codes. They're bit patterns. You can express those bit patterns as a number. But you're sending a pattern of bits, not a number. It's not like serial or something.

A -1 would be all 16 bits set to 1. That's exactly the same as 65535. Also written as 0xFFFF. If you want that to mean -1 to your receiver the that's something you have to code into the receiver.

Sure I understand that, but as I mentioned several times, -1 was just an example. The raw code that my receiver picked up was many different values alternating between + and -:

{3528, -1696, 440, -428, 440, -1292, 468, -400, 440, -424,.........}

But thanks I have the answer anyway, being that I need find a conversion logic.

Thanks all.

The raw code that my receiver picked up was many different values alternating between + and -:

If that is what it printed then you must have been storing those codes in an int instead of an unsigned int.

You should study up on how numbers are stored in a computer. These are 16 bit signed or unsigned int. So an unsigned 65535 is the same exact thing as a signed -1. Those two have the same bit pattern. It just matters which way you look at it. If you write

int i = 65535;
Serial.print(i);

It will print -1.

But at the end of the day it doesn't matter if you want to look at it signed or unsigned because what really matters is the bit pattern.

OK now I get it the pattern and I literally just found the function to convert to unsigned.

unsigned(myIntValue)

Seems to convert as I expected.

Thanks. :slight_smile: