![]() |
Forum Index : Microcontroller and PC projects : HEX$ problem
Author | Message | ||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Hi This might be a simple fix! I am converting this number--945736--to HEX using HEX$ The output I get is E6E48 The output I require is 0E6E48 as it forms the last 6 digits of an 8 digit hex number Is there any way to stop HEX$ dropping the first 0 Thanks |
||||
cwilt Senior Member ![]() Joined: 20/03/2012 Location: United StatesPosts: 147 |
I ran into this as well and did not find a simple solution besides converting to a string and adding the required number of zero's. x$=hex$(945736) y$=string$(8-len(x$),"0")+x$ I would be interested if someone has a better solution. |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Thanks cwilt I can do it that way but the initial number is entered by the user so the hex output changes I have been fighting this all afternoon! |
||||
mindrobots Newbie ![]() Joined: 21/05/2014 Location: United StatesPosts: 32 |
cwilt's solution might be the best for MMBASIC 4.5 Version 4.6 adds a 'chars' parameter to the BIN$, HEX$ and OCT$ to indicate number of characters in the output string and will pad with zeroes as needed. BIN$(number [,chars]) HEX$(number [,chars]) OCT$(number [,chars]) It appears Geoff knew you would have this problem and fixed it in advance! ![]() |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Thanks it sounds like that could be the solution is 4.6 due for release |
||||
cwilt Senior Member ![]() Joined: 20/03/2012 Location: United StatesPosts: 147 |
Good point mindrobots. I often forget that there are 2 different platforms, Maxi and Micro. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6269 |
Try this function. It should work the same on all versions of MM Basic. If you enter -1 for the number of characters the function returns the string padded to an even number of characters. DO
INPUT x PRINT myHEX$(x,6) PRINT myHEX$(x,8) PRINT myHEX$(x) PRINT myHEX$(x,-1) LOOP END FUNCTION myHEX$(number,chars) ' chars = 0 same as HEX$ ' chars > 0 pads to that number of characters ' chars < 0 pads to an even number of characters myHEX$=HEX$(number) IF chars > LEN(myHEX$) THEN myHEX$=STRING$(chars-LEN(myHEX$),"0")+myHEX$ ELSEIF chars < 0 AND LEN(myHEX$) MOD 2 > 0 THEN myHEX$="0"+myHEX$ ENDIF END FUNCTION Jim VK7JH MMedit |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
If you need it only once in your code a simple oneliner is: [code] Result$=RIGHT$("00000000" + HEX$(945736), 6) [/code] As a function: [code] FUNCTION ToHex$(number, minlength) ToHex$ = RIGHT$("00000000" + HEX$(number), minLength) END FUNCTION [/code] Microblocks. Build with logic. |
||||
cwilt Senior Member ![]() Joined: 20/03/2012 Location: United StatesPosts: 147 |
Nice alternative code. Will do some tests to see which is quicker. ![]() |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |