Two bytes to Integer without changing values
Moderator: Benj
-
- Posts: 342
- Joined: Fri Jan 26, 2018 12:30 pm
- Location: South Australia
- Has thanked: 43 times
- Been thanked: 60 times
- Contact:
Two bytes to Integer without changing values
Is it possible to save two bytes with values 4 and 15 to integer as 415?
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Two bytes to Integer without changing values
If the 15 is 1 instead are you expecting result to be 41 instead of 401?
Martin
-
- Posts: 342
- Joined: Fri Jan 26, 2018 12:30 pm
- Location: South Australia
- Has thanked: 43 times
- Been thanked: 60 times
- Contact:
Re: Two bytes to Integer without changing values
A good point Martin.
I need to save the data to sd-card as an integer.
The byte 1 can be 0 to 15 (as max).
The byte 2 can be 0 to 99.
------------------------------------
If byte 2 is 1 the result expected to be as 401.
I need to save the data to sd-card as an integer.
The byte 1 can be 0 to 15 (as max).
The byte 2 can be 0 to 99.
------------------------------------
If byte 2 is 1 the result expected to be as 401.
-
- Valued Contributor
- Posts: 1188
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Two bytes to Integer without changing values
If space was at a premium then you could save the data as a bit stream
- 7 bits would give you 0..127 and 4 bits 0..15.
Seems like a lot of extra work however - why not just save one value per byte (use << 8 and >> 8 to access the upper byte of you need to convert to a 16 bit integer) - the SD routines let you read a byte at a time - if the lower byte is less than 10 then pad with a leading 0
Martin
- 7 bits would give you 0..127 and 4 bits 0..15.
Seems like a lot of extra work however - why not just save one value per byte (use << 8 and >> 8 to access the upper byte of you need to convert to a 16 bit integer) - the SD routines let you read a byte at a time - if the lower byte is less than 10 then pad with a leading 0
Martin
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Two bytes to Integer without changing values
It would be case of Integer = of byte1 * 100 + byte2.viktor_au wrote:If byte 2 is 1 the result expected to be as 401.
What if byte 1 was 0 as the result would be just 1 if byte2 was one.viktor_au wrote:The byte 1 can be 0 to 15 (as max).
Assuming need to pad with 2 lots of 0's?
A full range expected results would be useful.
Martin
-
- Posts: 342
- Joined: Fri Jan 26, 2018 12:30 pm
- Location: South Australia
- Has thanked: 43 times
- Been thanked: 60 times
- Contact:
Re: Two bytes to Integer without changing values
Thank you mnf and medelec35
I tried to shift the values. Sure, can be done.
However the suggested by medelec35- Integer = of byte1 * 100 + byte2 works well.
------------------------
If:
byte1 = 4
byte2 = 15
-----------
.int1 = .byte1 * 100
.int2 = .byte2
.to_record = .int1 + .int2
------------
.to_record = 415
Thanks
I tried to shift the values. Sure, can be done.
However the suggested by medelec35- Integer = of byte1 * 100 + byte2 works well.
------------------------
If:
byte1 = 4
byte2 = 15
-----------
.int1 = .byte1 * 100
.int2 = .byte2
.to_record = .int1 + .int2
------------
.to_record = 415
Thanks