Converting int to uint8 and uint16

I am working on a certain project in which i am using ds1307 rtc lib master library. Actually what i want is to fetch the data from cclk of gsm and store it in ds1307 using that library. but i need to convert my variables from int to uint8 and uint16 as per the functions defined inside that library. So how can i convert int to uint in arduino?

Please, be specific as to your requirements.

1. DS1307 is a clock chip (Fig-1) whose time/calendar/alarm/status/control registers should not/can not be used as general purpose storage locations for keeping user variables.

Figure-1: DS1307 Chip

2. DS1307 RTC Module (Fig-2) comes with DD1307 chip + 24C32 EEPROM. A user may store his variables into this EEPROM of the DS1307 RTC Module.
ds1307Module.png
Figure-2: DS1307 RTC Module

3. You have desired the following conversions/transformations:
(1) an int type number (-32768 to 32767) to uint8_t (0 to 255). Correct?

(2) an int type number (-32768 to 32767) to uint16_t (0 to 65536). Correct?

ds1307chip.png

ds1307Module.png

GolamMostafa:
Please, be specific as to your requirements.

1. DS1307 is a clock chip (Fig-1) whose time/calendar/alarm/status/control registers should not/can not be used as general purpose storage locations for keeping user variables.
ds1307chip.png
Figure-1: DS1307 Chip

2. DS1307 RTC Module (Fig-2) comes with DD1307 chip + 24C32 EEPROM. A user may store his variables into this EEPROM of the DS1307 RTC Module.
ds1307Module.png
Figure-2: DS1307 RTC Module

3. You have desired the following conversions/transformations:
(1) an int type number (-32768 to 32767) to uint8_t (0 to 255). Correct?

(2) an int type number (-32768 to 32767) to uint16_t (0 to 65536). Correct?

yes correct. actually why i am converting this because you must have seen the rtc-lib master ds1307 library so in that datetime() class and rtc.adjust() class i need to push uint8 and uint16 datatype variables whereas i got int datatype variables in my code which i used to fetch cclk data from gsm

Hello RT009,

You can use a combination of structs and unions to combine and separate variables into individual bytes, something like this:

union TESTDATA {
  uint16_t data_01;
  struct {
    uint8_t data_0;
    uint8_t data_1;
  };
};
union TESTDATA testdata;

If you write a 16 bit value to testdata.data_01 then you can read its 2 bytes separately from testdata.data_0 and testdata.data_1 as required. You can do the same in reverse to combine the separate bytes back together. Play around with this to get what you need.

rt009:
yes correct.

So, you are saying that you want to convert/map your data from signed to unsigned as needed and then save them into the EEPROM of the DS1307 RTC Module. Can you give some typical examples of your gsm data?

PerryBebbington:
Hello RT009,

You can use a combination of structs and unions to combine and separate variables into individual bytes, something like this:

union TESTDATA {

uint16_t data_01;
  struct {
    uint8_t data_0;
    uint8_t data_1;
  };
};
union TESTDATA testdata;




If you write a 16 bit value to testdata.data_01 then you can read its 2 bytes separately from testdata.data_0 and testdata.data_1 as required. You can do the same in reverse to combine the separate bytes back together. Play around with this to get what you need.

But the OP has clearly said that he wants his signed data (range:-32768 to 32767) to be mapped to unsigned data (range: 0 to 255); can you please show an example how your union concept can map -10565 to 86?

What is the actual value range of your variable(s)?

Why did you decide on int type for this variable to begin with, rather than e.g. unsigned int (the same as uint16_t on the ATmega328p, other processors may differ) or byte (uint8_t), which is what you want for storing the value?

GolamMostafa:
But the OP has clearly said that he wants his signed data (range:-32768 to 32767) to be mapped to unsigned data (range: 0 to 255); can you please show an example how your union concept can map -10565 to 86?

You are correct of course. I put the example together quickly this morning before I went out to illustrate the principal. The same technique works with other variable types, you need to be aware of how many bytes in each type of variable and be aware that a union overlaps different variable into the same physical memory while a struct puts variables into contiguous locations in physical memory.

I need to convert my variables from int to uint8 and uint16 as per the functions defined inside that library.

From int (signed, 2 bytes) to uint8_t (two unsigned bytes):

union TESTDATA {
  int data_01;
  struct {
    uint8_t data_0;
    uint8_t data_1;
  };
};
union TESTDATA testdata;

From int (signed, 2 bytes) to uint16_t (unsigned, 2 bytes):

union TESTDATA {
  int sdata_01;
  uint16_t udata_01;
};
union TESTDATA testdata;

Combining everything into one union / struct:

union TESTDATA {
  int sdata_01;
  uint16_t udata_01;
  struct {
    uint8_t data_0;
    uint8_t data_1;
  };
};
union TESTDATA testdata;

E and OE.

I confess that when I first learnt C I found this totally confusing. If more explanation is needed please ask. If someone with a better knowledge of C thinks I've made a mistake please tell me and I'll correct it.

PerryBebbington:
You are correct of course.

From int (signed, 2 bytes) to uint8_t (two unsigned bytes):

The OP is saying int ---> uint8_t; you agreed with it. Then you are again saying: int --> 2 unsigned byes. It is confusing.

It's much easier to simply cast one type to another. Or bit shift to two uint8_t. Then you also actually get the variables in uint8_t and uint16_t which is what OP asked for:

  int myInt = 1234;
  uint16_t myUInt = (uint16_t)myInt;

  uint8_t highByte = myInt >> 8;
  uint8_t lowbyte = myInt && 0xFF;

I'm not sure if the results of this are what OP expects, because OP hasn't really said much about the variables such as whether they can actually contain negative values to begin with.

The OP is saying int —> uint8_t; you agreed with it. Then you are again saying: int → 2 unsigned byes. It is confusing.

The OP said:

I need to convert my variables from int to uint8 and uint16 as per the functions defined inside that library.

So, that’s int to uint8_t, which I gave and example of and int to uint_16, which I also gave an example of. Then I combined both into one union that does both jobs at the same time.

You can’t get an int into one unsigned byte, it won’t fit. You can get an int into 2 unsigned bytes, which is what this does. The OP also mentioned:

Actually what i want is to fetch the data from cclk of gsm and store it in ds1307 using that library

Based on that an what you said I took the OP to mean he wants to break an int, 2 bytes, into separate bytes so they can be stored in EEPROM. Of course the OP might have meant something else, but until he/she comes back and clarifies that is my working assumption.

Or bit shift to two uint8_t. Then you also actually get the variables in uint8_t and uint16_t which is what OP asked for:

What I said does that too, without all the shifting.

PerryBebbington:
What I said does that too, without all the shifting.

That bit shift is just to keep it simple and clear what's going on... and it's just one shift done. Very efficient as well, just a single instruction.