Shop OBEX P1 Docs P2 Docs Learn Events
Combine multiple byte data into single long — Parallax Forums

Combine multiple byte data into single long

dexxteritydexxterity Posts: 14
edited 2013-09-01 16:08 in Propeller 1
I have three memory locations stored as bytes in memory. I need help on how to combine them into one long. Here is an example of what I am looking to do:

Input:
byte1 = 7
byte_2 = 4
byte_3 = 9

Output:
long_1: 749

Comments

  • AribaAriba Posts: 2,690
    edited 2013-09-01 00:01
    Are the bytes single digits of a decimal number?
    Then you can just combine them with:
    long_1 := byte_1*100 + byte_2*10 + byte_3
    

    or will you combine them as bytes of the long like that:
    long_1.byte[0] := byte_1
    long_1.byte[1] := byte_2
    long_1.byte[2] := byte_3
    

    but this will not result in long_1 = 749 decimal. More like: long_1 = $09_04_07

    Andy
  • dexxteritydexxterity Posts: 14
    edited 2013-09-01 14:24
    I tried combining them into bytes in a long. It works pretty well, I think this should accomplish what I need. Thanks for the help!
  • dexxteritydexxterity Posts: 14
    edited 2013-09-01 14:44
    Okay here is an afterthought, so I feel like I am limiting the amount of data I can pack into one long - since I can only fit 4 bytes into one long. If I were able to combine the three bytes into one byte before packing it into the long then I would be able to fit more information. Is that possible or am I starting to get greedy?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-09-01 15:25
    dexxterity wrote: »
    Okay here is an afterthought, so I feel like I am limiting the amount of data I can pack into one long - since I can only fit 4 bytes into one long. If I were able to combine the three bytes into one byte before packing it into the long then I would be able to fit more information. Is that possible or am I starting to get greedy?

    I depends on the individual values of the bytes. A byte can have a value of zero to 255. If you're sure the three bytes wont exceed 255 then you could store them in a single byte. (Edit: See translation by Phil below.(Thanks Phil.))

    Is any one of the three byte ever going to exceed nine? You'd need to use a word to be sure to store any three digit value. If you can be sure the first digit is one or less, then you could safely use a byte to store your three digit number.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-09-01 15:28
    Duane Degn wrote:
    If you're sure the three bytes wont exceed 255 then you could store them in a single byte.
    I think what Duane meant to say is that if you're sure the product of the biggest values each of those bytes will contain does not exceed 255, then you can pack the values into a single byte.

    -Phil
  • dexxteritydexxterity Posts: 14
    edited 2013-09-01 15:45
    Duane Degn wrote: »
    Is any one of the three byte ever going to exceed nine?

    Yes they will be 0 - 59 (its clock data). It looks like I'll have to work with the three bytes into each long. Thanks for the help everyone.
  • tonyp12tonyp12 Posts: 1,951
    edited 2013-09-01 16:08
    HH-MM-SS: 0-23 5 bit + 0-59 = 6 bit + 0-59=6 bit.

    Total 17bit so you would need a long, unless 2 second count is good enough?

    Long := (HH<<12)+(MM<<6)+SS:

    word := (HH<<11)+(MM<<5)+SS/2:
Sign In or Register to comment.