•
•
•
•
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
![]() |
•
•
Join Date: May 2008
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: May 2008
Posts: 78
Reputation:
Rep Power: 1
Solved Threads: 7
•
•
•
•
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.
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 ...
I ment to call myself WilliamHemsworth ...
•
•
Join Date: Apr 2008
Posts: 274
Reputation:
Rep Power: 1
Solved Threads: 37
hi,
Maybe it s that what you are looking for
krs,
tesu
"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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Member Operators and Automatic type Conversions (C++)
- Conversions (C++)
- Problems with changing values returned by RegQueryValueEx (C)
- Number formatting/Help with doubles (Java)
- Are You Using These Keyword Selection Guidelines to Improve Website Conversion Rates? (Search Engine Optimization)
Other Threads in the C++ Forum
- Previous Thread: How to start for this?
- Next Thread: Error: Reading from file - using eof


Linear Mode