User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 373,192 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,811 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 284 | Replies: 5 | Solved
Reply
Join Date: May 2008
Posts: 8
Reputation: marcelomdsc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
marcelomdsc marcelomdsc is offline Offline
Newbie Poster

Number Conversions

  #1  
May 11th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 78
Reputation: n1337 is on a distinguished road 
Rep Power: 1
Solved Threads: 7
n1337 n1337 is offline Offline
Junior Poster in Training

Re: Number Conversions

  #2  
May 11th, 2008
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:

http://www.wikihow.com/Convert-from-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!
Last edited by n1337 : May 11th, 2008 at 7:38 pm.
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 238
Reputation: williamhemswort has a spectacular aura about williamhemswort has a spectacular aura about 
Rep Power: 2
Solved Threads: 31
williamhemswort's Avatar
williamhemswort williamhemswort is offline Offline
Posting Whiz in Training

Re: Number Conversions

  #3  
May 12th, 2008
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;
}
(on holiday)
I ment to call myself WilliamHemsworth ...
Reply With Quote  
Join Date: May 2008
Posts: 8
Reputation: marcelomdsc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
marcelomdsc marcelomdsc is offline Offline
Newbie Poster

Re: Number Conversions

  #4  
May 12th, 2008
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.
Reply With Quote  
Join Date: Apr 2008
Posts: 274
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 37
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Number Conversions

  #5  
May 12th, 2008
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
Last edited by tesuji : May 12th, 2008 at 6:20 pm.
Reply With Quote  
Join Date: May 2008
Posts: 8
Reputation: marcelomdsc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
marcelomdsc marcelomdsc is offline Offline
Newbie Poster

Re: Number Conversions

  #6  
May 13th, 2008
thanks to tesuji for the answer.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:58 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC