computer organization
c m s c 311  
f a l l   2 0 0 2  

Purpose

The goal of this project is to have you implement code to

Location of File

Go to ~chf11001/Proj/P1/, and there should be Convert.h. Create Convert.cpp.

Convert Specifications

Convert

Static Public Methods

static std::string baseTenToBaseK( const std::string & num, int dstBase ) ;
Given parameter num, which is a C++ string representing a base ten number (e.g., "23" ), convert this to another base (e.g., base 16) as a string (e.g. "17"), using as few digits as possible in the resulting string (i.e., don't write "0017").

You may assume:

  • dstBase is between 2 and 36
  • num, when translated to base 10, can fit in an unsigned int of 32 bits.
  • num is unsigned (no negative signs).

Use lowercase letters "a" to stand for 10, "b" for 11, ..., and "z" to stand for 35.

You should convert the string to a base 10 unsigned int value, then divide by k, convert the remainder to a character, and create a string by concatenating onto an empty string.

static baseKToBaseTen( const std::string & num, int srcBase ) ;
Given parameter num, which is a C++ string representing a number (e.g., "1111" ), written in some base k (e.g., base 2), convert this to a string written in base 10 (e.g., "15"), using as few digits as possible in the resulting string (i.e., don't write "0015").

You may assume:

  • srcBase is between 2 and 36
  • num, when translated to base 10, can fit in an unsigned int of 32 bits.
  • num is unsigned (no negative signs).
  • num is valid (but may contain leading 0's).
  • "a" stands for 11, "b" for 12, ..., "z" for 35. However, they may be written in uppercase. (Hint: convert to lowercase using transform).

To convert, you need to convert each digit (which is a character in the string) to a number, and multiply that number by some power of k, and sum, as shown in class. The resulting value should be converted to a string. Hint: use ostringstream by #include <sstream>.

static std::string srcToDstBase( const std::string & num, int srcBase, int dstBase ) ;
Given parameter str, which represents a number in srcBase, convert that to a number in dstBase. Assume both bases are between 2 and 36. The rules that affect the srcBase are the same as in baseKtoBaseTen. destBase should produce a string that uses only lowercase letters.

You can assume:

  • srcBase and dstBase are between 2 and 36 inclusive.
  • num may contain upper and lowercase letters and digits.
  • Your result should contain digits and lowercase letters, using as few characters as needed to represent the correct answer.
  • The value in srcBase once converted to base 10, can fit in an unsigned 32 bit value.
  • The values are unsigned.
Hint: use the previous two static methods to help out.
static std::string octalToBin( const std::string & num ) ;
static std::string hexToBin( const std::string & num ) ;
static std::string binToOctal( const std::string & num ) ;
static std::string binToHex( const std::string & num ) ;
You should convert these strings which represent values in base 2, 8, and 16, efficiently to one another. These strings can be arbitrarily long (i.e., they may not fit in 32 bit int values). Use the "quick" technique in class.

You can assume these are unsigned values.

Use lowercase to represent "a"..."f" (values 11 through 15).

static std::bool isBigEndian() ;
static std::bool isLittleEndian() ;
isBigEndian() returns true if the machine the code is run on is big endian, and false otherwise (i.e., it's little endian.

isLittleEndian() returns true if the machine the code is run on is little endian, and false otherwise (i.e., it's big endian.

This function should return different answers on the OIT cluster and the Sun machines on WAM. They have different endianness.

static int endian( unsigned int val, std:: string num ) ;
Assume num represents an unsigned base ten value written as a string. You will be given an unsigned int value. This value is really four consecutive bytes in memory. Determine if those four bytes represent num in big endian (return 1), little endian (return 0), or neither (return -1).

The idea is this. Suppose someone put 4 bytes into an unsigned int, but did not know how your machine endian was stored. They also sent a string indicating the number they meant to store ("num"), and based on that, you can determine what endian-ness the value is stored.

Hint: determine the number you have, then reverse the bytes, and determine that number. See if either one match the value. You know which endian-ness your machine is, so you should be able to figure it out.

Another trick is to take each of the four byte, cast it to an int, and shift the number of bits and sum. (Puzzling clue, I realize).

static std::string toOnesComp( int num, int numBits ) ;
static std::string toTwosComp( int num, int numBits ) ;
static std::string toSignedMag( int num, int numBits ) ;
static std::string toExcess( int num, int bias, int numBits ) ;
You are given a signed value called num. Convert num to a string with numBits characters, if possible. If the number of bits is too small, then return the string "OVERFLOW".

For example, if you were given num as -7, and written as one's complement, given 4 bits, your answer would be "1000". If it were 5 bits, you would return: "11000". If it were signed magnitude with 4 bits, you would return "1111". If you were asked to write -8 in one's complement, with 4 bits, you would return "OVERFLOW".

static int fromOnesComp( const std::string & num ) ;
static int fromTwosComp( const std::string & num ) ;
static int fromSignedMag( const std::string & num ) ;
static int fromExcess( const std::string & num, int bias ) ;
You're given a binary value written as a string, written in one of four representations. Convert that to a signed value. You may assume the value can be stored in a 32 bit signed int.
static std::string toIEEE754( std::string num ) ;
This is extra credit. Assume you have a string which may or may not have a leading - sign. This string represents a number written in decimal, e.g., "3.625". You should return a 32 character string representing the IEEE 754 single precision value.

You can assume the string does not generate repeated values, nor does it produce infinity or NaN.

The string should have the most significant bit (i.e., the sign bit) to the far left, and the least significant bit to the far right, and be 32 characters long.

The class Register should be ready soon.


See the class syllabus for policies concerning email
Last Modified: Fri Sep 27 19:41:52 EDT 2002
left up down right home

Web Accessibility