Skip to content

Casting a negative char to an int results in a positive number #6010

Closed
@MichaelBrunn3r

Description

@MichaelBrunn3r

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: ESP-12E
  • Core Version: 0dd6549
  • Development Env: Arduino IDE
  • Operating System: Windows

Settings in IDE

  • Module: Nodemcu
  • Flash Mode: ?
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • Reset Method: nodemcu
  • Flash Frequency: 40Mhz
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 115200

Problem Description

Casting a negative char to an int results in a positive number. The results differ depending on the platform.

Testing on device

Arduino Sketch

#include <Arduino.h>

void setup() {
  Serial.begin(115200);
  Serial.println();
  
  char c = -61;
  if(c != -61) { Serial.print("Char is not -61, but "); Serial.println((int)c); }
  else Serial.println("Char is -61");
  
  int i = c;
  if(i != -61) { Serial.print("Int is not -61, but "); Serial.println(i); }
  else Serial.println("Int is -61");
}

void loop() {
  delay(100);
}

Debug Messages

Running on Arduino IDE, Arduino Uno

Char is -61
Int is -61

Running on Arduino IDE, Nodemcu ESP-12E

Char is not -61, but 195
Int is not -61, but 195

Testing on host

Cloned newest release. Added file to TEST_CPP_FILES. Used make test to run.

Catch test file

#include <catch.hpp>
#include <iostream>

TEST_CASE("get char") {
    char c = -61;
    if(c != -61) std::cout << "Char is not -61, but " << (int)c << std::endl;
    else std::cout << "Char is -61" << std::endl;
    
    int i = c;
    if(i != -61) std::cout << "Int is not -61, but " << i << std::endl;
    else std::cout << "Int is -61" << std::endl;
}

Debug Messages

Char is -61
Int is -61

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions