Hi, this is my first time posting to this forum so please let me know if I'm doing anything wrong.
I would like to know how to do the following...
a. Write the internal representation of “17” in ASCII using two binary numbers
b. Write 17 and -17 in two’s complement notation. Use 8 bits for each
c. Write 0100101111112 in hexadecimal and 3F16 in binary

I would appreciate your help, thanks.

Hi, this is my first time posting to this forum so please let me know if I'm doing anything wrong.

I'm fairly new here also, so I can't tell ya...But as a general rule, I think it is best if you try to search for things on your own before posting...

a. Write the internal representation of “17” in ASCII using two binary numbers

I'm not totally sure what you mean by this, but you can get the ASCII character codes by using the following code:

``````cout << (int)'1' << endl;
cout << (int)'7' << endl;``````

In fact, you can find out any character code by replacing the 1 or 7 with whatever character you like. Anyway, the above code will give you 49 and 55, respectively. So I suppose you need to write these numbers in binary...As for doing that, there are several algorithms (to do it by hand), which you can mimic using code (if you need to). I'm sure there is also probably some function written in some library in C++ that will auto convert for you, or you could write one yourself (do a search if you desire). Anyway, the following link explains a few "by hand" algorithms for converting decimal to binary:

b. Write 17 and -17 in two’s complement notation. Use 8 bits for each

Again, quick google search will reveal how two's complement works. Here you go:

http://en.wikipedia.org/wiki/Two's_complement

c. Write 0100101111112 in hexadecimal and 3F16 in binary

I would suggest doing this in two steps, converting first to base ten (decimal) in both cases, and then from decimal to the appropriate base. However, there are also ways of converting directly from hex to binary and vice versa. (Remember, hex is base 16, so maybe you can find a pattern between groups of 4 bits and their hex equivalents...sorry if that sounds cryptic but again, there are tons of references and algorithms on the net, so it would be redundant for me to explain here...)

Good luck!

For C you can use these functions:

``````#include<iostream>
using namespace std;

typedef unsigned char UCHAR;

template<class t> inline
char *toBase(t val, char base, char *values) {
register char d = 1; t c;
register bool _signed = val < 0;

if (val >= 0) for (c = base; c <= val; c *= base, d++);
else for (c= -base; c >= val; c *= base, d++);

register char i = d + _signed;

char *bin = new char[i + 1];
if (_signed) bin[0] = '-';

for (val *= base; i - (_signed); i--)
bin[i - 1] = values[(val /= base, (
(val % base) < 0 ? -(val % base) : (val % base)))];

bin[d + _signed]='\0';
return bin;
}

inline char GetIndex(char *str, char c) {
for(UCHAR i = 0;; *str, i++)
if(*str++ == c) return i;
return 0;
}

template<class t> inline
t fromBase(char *val, char base, char *values) {
t v = 0;
for (char i=val[0]=='-';val[i];i++) {
v*=base;
v+=GetIndex(values, val[i]);
}
return val[0]=='-'?-v:v;
}

int main() {

// 0100101111111 in hex
int decimal = fromBase<int>("0100101111111", 2, "01");
char *hex = toBase(decimal, 16, "0123456789ABCDEF");
cout << hex << '\n';

// 3F16 in binary
decimal = fromBase<int>("3F16", 16, "0123456789ABCDEF");
char *bin = toBase(decimal, 2, "01");
cout << bin << '\n';

cin.ignore();
return 0;
}``````

thanks a lot william, but I don't need the functions.
I just need the results, it's for a C++ class assignment.
I'm not expecting anyone to do it for me but if someone could explain me how would I go about converting them, that would be great.

Thanks again though for all that.

hi,

``````"17" ascii ?
"1"  = 41hex   (4th column, 1st row)  = 0100  0001bin
"7"  = 47hex   (4th column, 7th row)  = 0100  0111bin

17
76543210
00010001  = 16 + 1  = 2^4  + 2^0

-17
00010001
11101110   (invert all digits --> this is one s complement)
+      1       (add 1 to get two s compl.)
-----------
11101111

0100  1011  1111    (form groups of 4 digits, starting at right)
4     B     F

3       F
0011    1111``````

Maybe it s that what you are looking for :)

krs,
tesu

thanks to tesuji for the answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.