Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 20:31 16 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : HEX$ problem

Author Message
edu001
Regular Member

Joined: 14/07/2014
Location: United Kingdom
Posts: 82
Posted: 10:25am 04 Nov 2014
Copy link to clipboard 
Print this post

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 States
Posts: 147
Posted: 10:32am 04 Nov 2014
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 82
Posted: 10:39am 04 Nov 2014
Copy link to clipboard 
Print this post

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 States
Posts: 32
Posted: 10:51am 04 Nov 2014
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 82
Posted: 11:05am 04 Nov 2014
Copy link to clipboard 
Print this post

  mindrobots said   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!


Thanks it sounds like that could be the solution is 4.6 due for release
 
cwilt
Senior Member

Joined: 20/03/2012
Location: United States
Posts: 147
Posted: 11:08am 04 Nov 2014
Copy link to clipboard 
Print this post

Good point mindrobots.

I often forget that there are 2 different platforms, Maxi and Micro.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5923
Posted: 01:07pm 04 Nov 2014
Copy link to clipboard 
Print this post

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   MMBasic Help
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:46am 06 Nov 2014
Copy link to clipboard 
Print this post

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 States
Posts: 147
Posted: 07:21am 06 Nov 2014
Copy link to clipboard 
Print this post

Nice alternative code. Will do some tests to see which is quicker.

  TZAdvantage said   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]

 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024