Notifications
Clear all

Pack 2 byte Integer  

  RSS

JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2019-10-01 11:18 pm  

I want to store a lot of data and I want to conserve Memory as much as I can.

What I want to store will be an unsigned Integer of always <=255.

Rather than store 2 bytes of the unsigned Integer, I would like to store a 1 byte Char$ (or something) instead.  A savings of 50%, which is a lot. 

How can I do it using the Arduino C++ language?

In Basic, if I remember OK, it would be fairly simple:

"IntegerIn" with a value of <=255.

WK$=char$(IntergerIn)  (this is wrong & I don't remember what's right)

Objective:  OneByte$ with the binary value of <=255

OneByte$=Right$(WK$,1)  or  OneByte$=Left$(WK$,1)

Done!

Store OneByte$ instead of Two Byte integer...

How do I do that with this language?

Thank you very much...

ps:  Getting it back to integer would be simple (I think):

TwoByteInteger=val(OneByte$)  (or something like that)

 

 

This topic was modified 12 months ago 2 times by JoeLyddon

Have Fun,
Joe Lyddon

www.woodworkstuff.net


Quote
starnovice
(@starnovice)
Estimable Member
Joined: 1 year ago
Posts: 168
2019-10-02 12:20 am  

How about:

Intcombines= int1*255 + int2

and

int2 = Intcombined mod 255

int1 = Intcombined / 255

Pat Wicker (Portland, OR, USA)


ReplyQuote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2019-10-02 6:53 am  

S O L U T I O N <=====

Here is the little Test Sketch (program, to me) that shows how I did it.

============== Start ================

/* PackInt
Testing the process of Creating 255 one byte records that contain the binary values of 1 to 255.

In this test, I counted an unsigned integer, from 1 to 255, Creating a one byte record containing the binary values from the Int Counter.

Then printing the final byte... int(byte)...

Was very simple... This is how I did it.

By Joe Lyddon,  10-1-2019

*/

// PackInt function
unsigned int intwk=0;   // the main Counter, 1-255, to be used to make the one byte record.
String byte1=" ";      // the one byte to contain the final result values 1-255.
int i=0;                      // while loop - Control.

void setup() {
Serial.begin(9600);
}

// =================================
void loop()
{

// Serial.println("PackInt Testing");

unsigned int intwk=0;
String byte1=" ";
byte1=" ";
intwk=0;
int i=0;
delay(1000);
PackInt(); // Perform the main routine.

}

// ===============================
void PackInt() {
// PackInt Testing <===================
intwk=0;
i=1;

while (i<=255) {
//================================
intwk=intwk+1;
byte1=lowByte(intwk);    // Extract the Integer Byte into a String Byte...

                                                 //  The Key!
//================================
// Print the test rsults to screen.
Serial.print("intwk= ");
Serial.print(intwk);                // Print integer Counter.
Serial.print(" int(byte1)= ");
Serial.print(byte1);               // Print the string byte.
Serial.println(" ");
delay(100);
i=i+1;
}

}

============== End ================

It took a little digging...  but, I think I found it and it appears to be working as desired.

Now, I can use it in my main project.

Thank you...

 

This post was modified 12 months ago 2 times by JoeLyddon

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer Moderator
Joined: 1 year ago
Posts: 1788
2019-10-04 8:13 am  

@joelyddon

Here's another solution Joe. 

Just use the cast function to cast from integer to byte.   No need to use strings this way.

int intwk;
byte intByte;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i<256; i++)
  {
     intwk = i;
     intByte = (byte)intwk;  // This line casts from int to byte.
     Serial.print("intwk = ");
     Serial.print(intwk);
     Serial.print("       ");
     Serial.print("intByte = ");
     Serial.println(intByte);
     delay(100);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

DroneBot Workshop Robotics Engineer
James


ReplyQuote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2019-10-04 8:49 pm  

@robo-pi

Thank you James...

I thought something was Fishy when I could just Print The Byte regardless if it was String or Integer data!!

 

Your for/next starts with a '0' record, which I don't want...  just 1-255...  (yeah just a picky item that doesn't mean anything)...  🙂  🙂 

My first try  WAS a For/Next loop & it didn't work right...(for some strange reason)...  changed it to a While & it worked great. 

I guess I don't need to specify LowByte...  Just Byte?!   I wasn't sure How the Integer was structured...  on the TRS80 Mod II, the bytes were in reverse order...  Here, I went for HighByte and LowByte....  High was always zero...  stayed with Low...  I guess Byte gets the Right-most Byte here, which is the same as LowByte; I don't remember seeing Byte...  I didn't know which one would be what I wanted... tried both...

Thank you very much  for your feedback.

Working on a Weather project Inside / Outside.   I will be able to store Temperature readings,  C*,  in ONE Byte (world wide Temp. no matter where it is)...  One byte... !  Same for Humidity if I wanted... still working it out...   I have also changed my mind about the OLED display and will try a LCD2004, 4 rows at 20 chars... first... I think I can make it work... Less memory/power demands.

Thanks again...  I appreciate it...  I'm still very NEW with this C++ language...  I'll get better...

 

 

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 478
2019-10-14 5:25 pm  

@joelyddon

Have you considered using Bit Shifting Operations and associated functions?

https://www.arduino.cc/reference/en/#variables


ReplyQuote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2019-10-14 6:11 pm  
Posted by: @frogandtoad

@joelyddon

Have you considered using Bit Shifting Operations and associated functions?

https://www.arduino.cc/reference/en/#variables

frogandtoad  (funny name...  how did you decide on this one?  🙂  )

Thank you for the question.

I didn't consider bit shifting...  I saw those instructions but, I felt I was after a byte...  as it turned out, getting That byte was a lot simpler than I thought... This final result is working just fine...  It's nice to know that those instructions are good, getting us down to the BIT level which many languages seldomly dig that deep.

Now, how would Bit Shifting make it any easier?

Thank you...

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote
ZeFerby
(@zeferby)
Reputable Member
Joined: 1 year ago
Posts: 380
2019-10-14 6:50 pm  

@joelyddon maybe that can help ?

Also note that bit shifting is usually a VERY QUICK operation for most chips.

void setup() {
// put your setup code here, to run once:
byte c10 = 23;
byte c20 = 145;



byte c1;
byte c2;
int i;
uint16_t u16;

Serial.begin(9600);

Serial.println("Combine/Extract two 8-bit positive integers into/from a 16-bit variable");

Serial.println();
Serial.println("Combination with (signed) int, either basic arithmetic or bit shifting operators : Booo !!");
c1 = c10; c2 = c20; i = 0; u16 = 0;
Serial.print("c1="); Serial.print(c1); Serial.print(" c2="); Serial.print(c2); Serial.print(" i="); Serial.print(i); Serial.print(" u16="); Serial.print(u16); Serial.println();
i = (c2 *256) + c1;
Serial.print(" i = (c2 *256) + c1 = "); Serial.print(i); Serial.println(" ==> Negative int result if c2 > 127 !!");
i = (c2 << 8) + c1;
Serial.print(" i = (c2 << 8) + c1 = "); Serial.print(i); Serial.println(" ==> Negative int result if c2 > 127 !!");

Serial.println();
Serial.println("---------------------------------------------");
Serial.println("Now using uint16_t : unsigned 16-bit integers");
Serial.println("---------------------------------------------");

Serial.println();
Serial.println("Combination with basic arithmetic operators :");
c1 = 23; c2 = 145; i = 0; u16 = 0;
Serial.print("c1="); Serial.print(c1); Serial.print(" c2="); Serial.print(c2); Serial.print(" i="); Serial.print(i); Serial.print(" u16="); Serial.print(u16); Serial.println();
u16 = (c2 * 256) + c1;
Serial.print(" u16 = (c2 *256) + c1 = "); Serial.print(u16); Serial.println(" ==> Unsigned result ok");
Serial.println("Extraction with basic arithmetic operators :");
c1 = 0; c2 = 0;
Serial.print("c1="); Serial.print(c1); Serial.print(" c2="); Serial.print(c2); Serial.print(" i="); Serial.print(i); Serial.print(" u16="); Serial.print(u16); Serial.println();
c2 = u16 /256;
Serial.print("-> c2 = u16a /256 = "); Serial.println(c2);
c1 = u16 - 256*(u16/256);
Serial.print("-> c1 = u16a - (256*c2) = u16a - 256*(u16a/256) = "); Serial.println(c1);
Serial.println("Not elegant, but works...");

Serial.println();
Serial.println("Combination with bit shifting operators : THE CLEANEST");
c1 = 23; c2 = 145; i = 0; u16 = 0;
Serial.print("c1="); Serial.print(c1); Serial.print(" c2="); Serial.print(c2); Serial.print(" i="); Serial.print(i); Serial.print(" u16="); Serial.print(u16); Serial.println();
u16 = (c2 << 8) | c1;
Serial.print("-> u16 = (c2 << 8) | c1 = "); Serial.print(u16); Serial.println(" ==> Unsigned result ok");
Serial.println("Extraction with bit shifting operators : THE CLEANEST");
c1 = 0; c2 = 0;
Serial.print("c1="); Serial.print(c1); Serial.print(" c2="); Serial.print(c2); Serial.print(" i="); Serial.print(i); Serial.print(" u16="); Serial.print(u16); Serial.println();
c1 = u16 & 0xFF;
Serial.print("-> c1 = u16 & 0xFF = "); Serial.println(c1);
c2 = u16 >> 8;
Serial.print("-> c2 = u16 >>8 = "); Serial.println(c2);


}

void loop() {
// put your main code here, to run repeatedly:

}

Eric


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 478
2019-10-15 2:30 am  

@joelyddon

"frog and toad" is aussie slang for hit the road / get going... haven't you ever heard the term: "I'm going to hit the frog and toad"?

Now... define easier? this is by no means hard 🙂

Advantages:
1) Bit shifting provides you full control over your (in this case) byte.
2) Bit shifting offers very fast execution.

ZeFerby has provided some good examples, and the link I provided also has some good examples, including some built in functions that make it very easy indeed.  Virtually all embedded programmers use bit shifting to great effect, due to working with very limited amounts of memory, which I thought was your goal?

 


ReplyQuote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2019-10-15 5:41 am  
Posted by: @frogandtoad

@joelyddon

"frog and toad" is aussie slang for hit the road / get going... haven't you ever heard the term: "I'm going to hit the frog and toad"?

Now... define easier? this is by no means hard 🙂

Advantages:
1) Bit shifting provides you full control over your (in this case) byte.
2) Bit shifting offers very fast execution.

ZeFerby has provided some good examples, and the link I provided also has some good examples, including some built in functions that make it very easy indeed.  Virtually all embedded programmers use bit shifting to great effect, due to working with very limited amounts of memory, which I thought was your goal?

 

Yes, that is my goal...  It seems like grabbing the byte I'm after was meeting my goal w/o several instructions.

I will have to study the Shifting method...  looks like a very powerful approach...

No, I had never heard of the term "I'm going to hit the frog and toad".   Funny...

Thank you very much...

Enjoy!

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 478
2019-10-15 6:07 am  

@joelyddon

No problem.

Personally, and unless you are truly running out of space, I would suggest getting the code working properly first, and then focus on it's robustness and readability.  If footprint or speed becomes a problem, only then should you focus on those things.

Once you have your algorithms and formulas sorted out, put them into your own functions to simplify the program logic.  Likewise, if your code is large enough, you can develop some classes to..., well... add a touch of class 😉

 


ReplyQuote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2019-10-15 6:20 am  

@frogandtoad

Sounds good...  I think that is what I was doing..

Here, I was just after the one byte which I could manipulate to code all Temp. readings into one byte instead of two or three...  and I think I met that goal...  I will eventually post my routine of doing that...

I don't know if I'm really Memory poor or not...  It's just a thought I had... and it seems to be working out.

I've been working on the rest of the project...  still waiting for parts...

I have the DS3231 to work with...  one major part of the project I have to work out...  I am working on it...

Thank you!

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote
frogandtoad
(@frogandtoad)
Reputable Member
Joined: 1 year ago
Posts: 478
2019-10-16 4:37 am  

@joelyddon

Oh, I almost forgot... to save even more space, you can also look into using the preprocessor to #define some constants.

 


ReplyQuote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2019-10-16 5:15 am  

@frogandtoad

Yes...  I have a lot more to learn...  how to be really efficient...  🙂

Thank you very much!

 

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote