RosettaCodeData/Task/Variable-length-quantity/XTalk/variable-length-quantity-2....

68 lines
2.2 KiB
Plaintext

function VLQtoWholeNum theHexVLQ
-- The number must be an integer between zero and 4,294,967,295
put baseConvert(theHexVLQ,16,2) into theBits
put 0 into bitCounter
put empty into the8bitBytes
repeat
if char 1 of theBits is not empty then
put char 1 theBits after the8bitBytes
delete char 1 of theBits
if theBits is empty then exit repeat
add 1 to bitCounter
if bitCounter=8 then
put "," after the8bitBytes
put 0 into bitCounter
next repeat
end if
else
exit repeat
end if
end repeat
put the number of items in the8bitBytes into y
repeat with x = 1 to y
put char 1 of item x of the8bitBytes into lengthCntrlBit
delete char 1 of item x of the8bitBytes
if the number of chars in item x of the8bitBytes < 7 then
repeat 7 - (the number of chars in item x of the8bitBytes)
put "0" before item x of the8bitBytes
end repeat
end if
put item x of the8bitBytes after WholeNumInBinary
switch lengthCntrlBit
case "1"
next repeat
break
case "0"
exit repeat
break
end switch
end repeat
return baseConvert(WholeNumInBinary,2,10)
end VLQtoWholeNum
function isHexString theString
---again there is probably an easier way to do this:
if char 1 to 2 of theString is "0x" then delete char 1 to 2 of theString
put the number of chars in theString into y
repeat with x = 1 to y
if char x of theString is not in "abcdefABCDEF0123456789" then
return false
end if
end repeat
end isHexString
on VLQHexToWholeNum
Ask "Enter Variable Length Quantity Hex Value:" -- input dialog
if it is not empty then
if char 1 to 2 of it is "0x" then delete char 1 to 2 of it
put it into hexString
if isHexString(hexString) is false then
answer "Only Valid Hex Digits Are Allowed!"
exit VLQHexToWholeNum
else
put "Original Variable Length Quantity in Hex="& hexString & cr & \
"Whole Number=" & VLQtoWholeNum(hexString) into fld "Output"
end if
end if
end VLQHexToWholeNum