mki and mks functions from good old QBasic (Sample Programs)

I got an email about adding the MKI and MKS functions to BASIC-256, so I wrote functions you can use (and modify) in BASIC-256 to do it.

# mki\$ and mks\$ functions
# to convert an integer to a 2 byte string and
# a float to a four byte IEEE754 single
#
# 2015-01-15 j.m.reneau
#
# does not handle overflow, underflow or signed ints (mki\$)

function mki\$(i)
# build a little-endian string (2 bytes) of an unsigned integer
return chr(i%256)+chr(i\256%256)
end function

function mks\$(f)
# build a string of binary 32 binary digits in IEEE754
# float and then convert to 4 characters
# there has to be a better way but???
# sign bit
if f < 0 then
d\$ = "1"
f=f*-1
else
d\$ = "0"
endif
# 8 bit exponent
xp = 127
while f < 1 and xp>0
f*=2
xp--
end while
while f > 2 and xp<256
f/=2
xp++
end while
d\$ += right("00000000"+tobinary(xp),8)
# mantissa (23 bit)
f = abs(f-1)
while f>0 and length(d\$)<32
f*=2
if f >= 1 then
d\$ += "1"
f--
else
d\$ += "0"
endif
end while
d\$ = left(d\$+"00000000000000000000000",32)
print d\$
return chr(frombinary(mid(d\$,1,8))) + chr(frombinary(mid(d\$,9,8))) + chr(frombinary(mid(d\$,17,8))) + chr(frombinary(mid(d\$,25,8)))
end function

function str2hex\$(s\$)
x\$ = ""
for i = 1 to length(s\$)
x\$ += right("0"+tohex(asc(mid(s\$,i,1))),2)
next i
return x\$
end function

a\$ = mki\$(1)+mki\$(65535)+mki\$(12345)
print str2hex\$(a\$)

a\$ = mks\$(123456)
print str2hex\$(a\$)