Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Split Integer into 2 bytes

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 517

blank.gif
PostPosted: Fri Nov 14, 2014 8:19 pm    Post subject: Split Integer into 2 bytes Reply with quote

Ok, so why doesn't this work?

Code:

$regfile = "m128def.dat"
$hwstack = 800
$swstack = 600
$framesize = 100
$crystal = 16000000
$baud = 9600

 Dim TempInteger As Integer
 Dim TempByte As Byte
 Dim TempByte2 As Byte

TempInteger = 1006

TempByte = High(TempInteger)
TempByte2 = Low(TempInteger)

Print Tempbyte ; TempByte2
 


Result is 3238

I was expecting 1006?



(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5199
Location: Holland

blank.gif
PostPosted: Fri Nov 14, 2014 8:57 pm    Post subject: Reply with quote

numbers of base 10 system which we use, can not be stored in a computer with base 10 since the computer only knows 0 and 1.
So numbers are binary coded. And we group 8 bits into a byte. For 8 bits, we can represent maximum 255 : 1111_1111 : 128+64+32+16+8+4+2+1
If we want bigger numbers for an integer/word we use 2 bytes. The LSB is still representing 0-255 but the MSB has a different weight : 256+512+1024+2048 etc.
If you use low() you get the LSB. If you use high() you get the MSB. But when printing these bytes, they loose their weight/significance. And you print as a decimal number.
So 3 means 3*256 and 238 need to be added : (3*256) + 238 which should make 1006.
Use the simulator to have a look at the bits and bytes.

consider what you was expecting for values like 32000 and 3271 ?

_________________
Mark
Back to top
View user's profile Visit poster's website
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 517

blank.gif
PostPosted: Fri Nov 14, 2014 10:00 pm    Post subject: Reply with quote

I didn't take into account the weighting issue when trying to print those values.

My main concern is that an integer with a value of 1006 can be split into 2 bytes with actual values of 10 and 6 respectively. Are you saying low() and high() do that but that can't actually be printed but the bytes do contain the correct values for other uses?
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5199
Location: Holland

blank.gif
PostPosted: Fri Nov 14, 2014 10:13 pm    Post subject: Reply with quote

when you use someInt=1006, it will be assigned with a binary number, the only number that can be used. have a look in the simulator.
1006 is just like we humans want to see a number. when you use INPUT , this decimal number is converted into binary. and when you use PRINT, this binary number is converted into a decimal string.
you can use str() to get a string, and then use left/right/mid or str2digits.

_________________
Mark
Back to top
View user's profile Visit poster's website
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 517

blank.gif
PostPosted: Fri Nov 14, 2014 10:27 pm    Post subject: Reply with quote

Thanks Mark

I was hoping to have smaller compiled code than using strings but I guess it is what it is......
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1915

blank.gif
PostPosted: Fri Nov 14, 2014 11:18 pm    Post subject: Reply with quote

A = I \ 100
B = I MOD 100
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 517

blank.gif
PostPosted: Fri Nov 14, 2014 11:36 pm    Post subject: Reply with quote

MWS wrote:
A = I \ 100
B = I MOD 100


Nope

Produces "2" and "38"
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1915

blank.gif
PostPosted: Fri Nov 14, 2014 11:54 pm    Post subject: Reply with quote

If 1006 divided by 100 gives you 2, you do something seriously wrong.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5199
Location: Holland

blank.gif
PostPosted: Fri Nov 14, 2014 11:57 pm    Post subject: Reply with quote

if you want integer division, the assigned variable need to be an integer too.
_________________
Mark
Back to top
View user's profile Visit poster's website
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 517

blank.gif
PostPosted: Sat Nov 15, 2014 12:34 am    Post subject: Reply with quote

albertsm wrote:
if you want integer division, the assigned variable need to be an integer too.


Indeed

TempInteger2 = TempInteger \ 100
Byte1 = TempInteger2
TempInteger2 = TempInteger MOD 100
Byte2 = TempInteger2

Works just fine (and saves quite a bit of compiled space compared to handling the string equivalent)

Danke!
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum