Home:ALL Converter>Protocol Buffers handle uint64 and uint32 data-types

Protocol Buffers handle uint64 and uint32 data-types

Ask Time:2021-10-12T13:28:03         Author:Dark Sorrow

Json Formatter

I am developing a unified protocol serializer for communication between different type of devices using protobuf3.
Different type of devices/environment

  1. Micro-Controller (Cortex M-4 and M-33 (planned) with Ethernet connectivity (Uses Embedded C/C++)
  2. Flutter (Android + iOS + Web)
  3. Python based web-server (using DRF)
  4. Data Gateway (Linux + C/C++)
  5. Android + Kotlin (Legacy Apps)

Most data I send will be of the following types : uint32, uint64, string and bytes.

I have some confusion for Scalar Value Types. If I use uint64 for serial number then in C++ I get uint64 but for Kotlin I get long (signed) and for Python I get int/long.

For C++ I believe I will get range 0 to 18,446,744,073,709,551,615.
For Kotlin and Python I believe the range is 0 to 9,223,372,036,854,775,807.
What will be the range for Flutter?
Looks like I will have a range capped to 9,223,372,036,854,775,807 for my entire system. Am I right regarding this?
I know that serial number will not reach such high value but I will need handle such condition in code.
For checksum CRC32 value I am using uint32 data type which can be anything in range 0 to 4,294,967,295 and the system should be able to handle this. How do I handle this? Kotlin uses int for uint32 while python uses int/long.

[2] In Java, unsigned 32-bit and 64-bit integers are represented using their signed counterparts, with the top bit simply being stored in the sign bit.

[4] 64-bit or unsigned 32-bit integers are always represented as long when decoded, but can be an int if an int is given when setting the field. In all cases, the value must fit in the type represented when set. See [2].

syntax = "proto3";

message Request{
    uint64 serial_number;
    ...
    uint32 checksum;
}

Author:Dark Sorrow,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69535475/protocol-buffers-handle-uint64-and-uint32-data-types
yy