Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Base conversion function using SML. First, you need to build this function: val convert = fn : int * int -> string This

Create a Base conversion function using SML.

First, you need to build this function:

val convert = fn : int * int -> string 

This function takes a tuple of two integers. The first integer is the number to be converted. The second integer is the base into which it is converted. The output is a string. You have to support the following bases: 16, 10, 8, 4 and 2.

Assume that the input is a 16-bit unsigned integer (meaning the possible values range from 0 to 65,535. You don't have to handle any values outside that range.

An example of the raw output of convert would look like this:

convert (25, 16); val it = "19" : string convert (25, 2); val it = "11001" : string convert (25, 10); val it = "25" : string 

You also need to build these functions.

val decstr = fn : int -> string val binstr = fn : int -> string val quastr = fn : int -> string val octstr = fn : int -> string val hexstr = fn : int -> string

These functions take an integer as input and produce a string. decstr converts to decimal (aka, base 10), binstr converts to binary (aka, base 2), quastr converts to base 4, octstr converts to octal (aka, base 8), and hexstr converts to hexadecimal.

To make the outputs clear, the string outputs for these functions start with a different pair of characters, depending on the base being converted into. Here are the first two characters of these functions: decstr, "0d"; binstr, "0b"; quastr, "0q"; octstr, "0o"; hexstr, "0x".

Finally, you need to pad the string with leading zeros, appropriate to a 16 bit representation. That means that binstr will always produce 16 characters after "0b", quastr will always produce 8 characters after "0q", octstr will always produce 6 characters after "0o", decstr will always produce 5 characters after "0d", and hexstr will always produce 4 characters after "0x".

To making testing easy, you will use the following function to drive all the other functions using a single input.

fun numstr n = (binstr n, quastr n, octstr n, decstr n, hexstr n);

Here's what your function will look like when it runs correctly.

numstr 25; val it = ("0b0000000000011001","0q00000121","0o000031","0d00025","0x0019") : string * string * string * string * string numstr 2652; val it = ("0b0000101001011100","0q00221130","0o005134","0d02652","0x0A5C") : string * string * string * string * string

Recommendations:

Do this systematically.

Use stubs in place of functions you haven't written yet.

Get one small piece working, then go on to the next.

Don't try to bolt the whole thing together and then debug the whole thing in one messy go

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: David M. Kroenke

5th Edition

B000CSIH5A, 978-0023668814

More Books

Students also viewed these Databases questions

Question

Discuss the main advantages of payroll outsourcing.

Answered: 1 week ago