# Learning to write a function

Discussion in 'Oshonsoft' started by GreyHairOldGuy, Dec 22, 2014.

1. ### GreyHairOldGuyNew Member

Joined:
May 30, 2011
Messages:
12
Likes:
0
Location:
I feel pretty dumb - you guys are obviously so far above me in programming........
..........I got my printout in hex working, thanks to Ian and others.
My first approach was VERY crude, but it worked.
I would really appreciate some ideas on how YOU would do it.
ALSO
I am now trying to create my first function and after working on it all day, I am still stuck on the first line.
I come in with an 8 bit byte and leave with a 16 bit word.
I can either define the byte or the word but cannot seem to figure out how to define both.
(Please promise not to laugh) My code is below:

'=========================================================
'FUNCTIONS - FUNCTIONS - FUNCTIONS - FUNCTIONS - FUNCTIONS
'=========================================================
Function hextodsp(wout As Word) As Word, (binx As Byte) As Byte
Dim xhi As Byte
Dim xlo As Byte
xhi = binx Mod 16
If xhi = 0x0 Then wout.HB = 0x30
If xhi = 0x1 Then wout.HB = 0x31
If xhi = 0x2 Then wout.HB = 0x32
If xhi = 0x3 Then wout.HB = 0x33
If xhi = 0x4 Then wout.HB = 0x34
If xhi = 0x5 Then wout.HB = 0x35
If xhi = 0x6 Then wout.HB = 0x36
If xhi = 0x7 Then wout.HB = 0x37
If xhi = 0x8 Then wout.HB = 0x38
If xhi = 0x9 Then wout.HB = 0x38
If xhi = 0xa Then wout.HB = 0x41
If xhi = 0xb Then wout.HB = 0x42
If xhi = 0xc Then wout.HB = 0x43
If xhi = 0xd Then wout.HB = 0x44
If xhi = 0xe Then wout.HB = 0x45
If xhi = 0xf Then wout.HB = 0x46
xlo = binx / 16
If xlo = 0x0 Then wout.LB = 0x30
If xlo = 0x1 Then wout.LB = 0x31
If xlo = 0x2 Then wout.LB = 0x32
If xlo = 0x3 Then wout.LB = 0x33
If xlo = 0x4 Then wout.LB = 0x34
If xlo = 0x5 Then wout.LB = 0x35
If xlo = 0x6 Then wout.LB = 0x36
If xlo = 0x7 Then wout.LB = 0x37
If xlo = 0x8 Then wout.LB = 0x38
If xlo = 0x9 Then wout.LB = 0x38
If xlo = 0xa Then wout.LB = 0x41
If xlo = 0xb Then wout.LB = 0x42
If xlo = 0xc Then wout.LB = 0x43
If xlo = 0xd Then wout.LB = 0x44
If xlo = 0xe Then wout.LB = 0x45
If xlo = 0xf Then wout.LB = 0x46
End Function

2. ### leftfield95Member

Joined:
Aug 5, 2008
Messages:
71
Likes:
2
Location:
lincs, UK
You seem to be wrongly using the function statement, try this example.

Code (text):
Dim wout As Word
Dim your_byte_in_here As Byte

main:

For your_byte_in_here = 0 To 255
wout = hextodsp(your_byte_in_here)
Next your_byte_in_here

Goto main
End

Function hextodsp(binx As Byte) As Word
Dim xhi As Byte
Dim xlo As Byte
xhi = binx Mod 16
If xhi = 0x0 Then hextodsp.HB = 0x30
If xhi = 0x1 Then hextodsp.HB = 0x31
If xhi = 0x2 Then hextodsp.HB = 0x32
If xhi = 0x3 Then hextodsp.HB = 0x33
If xhi = 0x4 Then hextodsp.HB = 0x34
If xhi = 0x5 Then hextodsp.HB = 0x35
If xhi = 0x6 Then hextodsp.HB = 0x36
If xhi = 0x7 Then hextodsp.HB = 0x37
If xhi = 0x8 Then hextodsp.HB = 0x38
If xhi = 0x9 Then hextodsp.HB = 0x38
If xhi = 0xa Then hextodsp.HB = 0x41
If xhi = 0xb Then hextodsp.HB = 0x42
If xhi = 0xc Then hextodsp.HB = 0x43
If xhi = 0xd Then hextodsp.HB = 0x44
If xhi = 0xe Then hextodsp.HB = 0x45
If xhi = 0xf Then hextodsp.HB = 0x46
xlo = binx / 16
If xlo = 0x0 Then hextodsp.LB = 0x30
If xlo = 0x1 Then hextodsp.LB = 0x31
If xlo = 0x2 Then hextodsp.LB = 0x32
If xlo = 0x3 Then hextodsp.LB = 0x33
If xlo = 0x4 Then hextodsp.LB = 0x34
If xlo = 0x5 Then hextodsp.LB = 0x35
If xlo = 0x6 Then hextodsp.LB = 0x36
If xlo = 0x7 Then hextodsp.LB = 0x37
If xlo = 0x8 Then hextodsp.LB = 0x38
If xlo = 0x9 Then hextodsp.LB = 0x38
If xlo = 0xa Then hextodsp.LB = 0x41
If xlo = 0xb Then hextodsp.LB = 0x42
If xlo = 0xc Then hextodsp.LB = 0x43
If xlo = 0xd Then hextodsp.LB = 0x44
If xlo = 0xe Then hextodsp.LB = 0x45
If xlo = 0xf Then hextodsp.LB = 0x46
End Function

3. ### GreyHairOldGuyNew Member

Joined:
May 30, 2011
Messages:
12
Likes:
0
Location:
Thank You
I see what you did - the function name hextodsp is actually the output word, or at least contains it.

Using 32 "If" statements is dumb, but was a first pass at the problem and it worked.

What is a better way?

Mike

Joined:
Jan 12, 1997
Messages:
-
Likes:
0

5. ### jjwMember

Joined:
Apr 16, 2012
Messages:
327
Likes:
17
Location:
Helsinki, Finland
ONLINE

Use LookUp function.
hextodsp = LookUp ( 0x30, 0x31, ... 0x41, ... 0x46 ), x
where x is 0 to 15

6. ### PommieWell-Known MemberMost Helpful Member

Joined:
Mar 18, 2005
Messages:
10,369
Likes:
368
Location:
Brisbane Australia
A much simpler and efficient way would be,
Code (text):

if x<0x0a then
hextodsp.HB = 0x30+x
else
hextodsp.HB = 0x41-10+x

Sudo code as I don't know oshonsoft syntax.

Mike.

Joined:
Mar 28, 2011
Messages:
9,525
Likes:
937
Location:
Rochdale UK
ONLINE
Good enough!! Just an endif to add!!

8. ### leftfield95Member

Joined:
Aug 5, 2008
Messages:
71
Likes:
2
Location:
lincs, UK
Based on the OP's original function this is what I did.

Code (text):

Function hextodsp(binx As Byte) As Word
Dim xhi As Byte
Dim xlo As Byte
xhi = binx Mod 16
hextodsp.HB = 0x30 + xhi
xlo = binx / 16
hextodsp.LB = 0x30 + xlo
End Function

Joined:
Mar 28, 2011
Messages:
9,525
Likes:
937
Location:
Rochdale UK
ONLINE
Wrong way round.... The OP wants to display a hex value not binary to ASCII... No division needed, As Mike has already shown we need ASCII A~F as well...

10. ### GreyHairOldGuyNew Member

Joined:
May 30, 2011
Messages:
12
Likes:
0
Location:
Wow!
I want to look at and try your other suggestions as well, because if nothing else, I'll learn something useful for later.

THANK YOU very much everyone for helping me learn how to do this.

My one quick question - is there a line continuation character in Oshonsoft? I think I looked EVERYWHERE.
Another package I used to use allowed _ to be used as a line continuation but it didn't seem to work for me here.

This is what I came up with - I did 8 to 16 and 16 to 30 bit Hex to ASCII conversion Functions:

'FUNCTIONS - FUNCTIONS - FUNCTIONS - FUNCTIONS - FUNCTIONS
'=========================================================
'Function h8to16a
'Input hex2 Two Hex Characters total 8 Bit BYTE
'Output h8to16a Two ASCII Characters total 16 Bit WORD
'=========================================================
Function h8to16a(hex2 As Byte) As Word 'Define Function
Dim tmp As Byte 'Temporary Storage for High/Low Hex Digit
tmp = hex2 Mod 16 'Extract Lower Hex Digit
h8to16a.HB = LookUp("0123456789ABCDEF"), tmp 'Convert to ascii & Store Upper
tmp = hex2 / 16 'Extract Upper Hex Digit
h8to16a.LB = LookUp("0123456789ABCDEF"), tmp 'Convert to ascii & Store Lower
End Function
'=========================================================
'Function h16to32a
'Input hex4 Four Hex Characters total 16 Bit WORD
'Output h8to16a Four ASCII Characters total 32 Bit LONG
'=========================================================
Function h16to32a(hex4 As Word) As Long 'Define Function
Dim txp1 As Byte 'Temporary Storage for the Hex Digits
Dim txp As Byte 'Root Temporary Storage for the Hex Digits
txp1 = hex4.LB 'Get Bottom 8 Bits-------------------
txp = txp1 Mod 16 'Extract Lower Hex Digit
h16to32a.4B = LookUp("0123456789ABCDEF"), txp 'Convert to ascii & Store Lower
txp = txp1 / 16 'Extract Upper Hex Digit
h16to32a.3B = LookUp("0123456789ABCDEF"), txp 'Convert to ascii & Store Lower
txp1 = hex4.HB 'Get Upper 8 Bits---------------------
txp = txp1 Mod 16 'Extract Lower Hex Digit
h16to32a.HB = LookUp("0123456789ABCDEF"), txp 'Convert to ascii & Store Lower
txp = txp1 / 16 'Extract Upper Hex Digit
h16to32a.LB = LookUp("0123456789ABCDEF"), txp 'Convert to ascii & Store Lower
End Function

Merry Christmas everyone............Mike