|
-
Apr 7th, 2001, 06:15 PM
#1
Thread Starter
Hyperactive Member
-
Apr 7th, 2001, 06:23 PM
#2
Monday Morning Lunatic
Things like this are a lot easier to do if you think of your standard C string as an array:
Code:
char *pcBin = "10101";
int iTemp = 0'
for(int i = 0; i < strlen(pcBin); i++) {
if(pcBin[i] == '1')
iTemp += 1 << i;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 7th, 2001, 06:49 PM
#3
Thread Starter
Hyperactive Member
Hrmm
Than Parksie, but how come i dont get the same answer with the function u gave me?
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 06:52 PM
#4
Thread Starter
Hyperactive Member
Also
What would :
iTemp = iTemp + (2^i)
be in c++?
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 06:55 PM
#5
Monday Morning Lunatic
You use the shift operators << and >> 
For example 1 << 2 = 1 * 2^2
It's much easier to see when you look in binary. They shift everything one bit to the left or the right:
b0101010 << 1 = b1010100
b0101010 >> 3 = b0000101
This is why 2^i disappeared in my code. Anyway, what "different answer" did you get?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 7th, 2001, 07:05 PM
#6
Thread Starter
Hyperactive Member
Let me explain
I am writing a dll that will send data to the parallel port. The functions in the dll will then be used in VB. I already have the funcs to do the conversions in vb, but i want to include them into my dll. (these funcs were the ones i posted in the Parallel Port-Urgent" thread. I AM USING 8-BIT BINARIES.
I thought
00000001 was 1
00000010 was 2
00000011 was 3
00000100 was 4
00000101 was 5
etc...
When i use the code you gave me when I put
00000101 it shows 6
or for:
11111111 it shows 0 instead of 255.
For example, i would get 255 for 11111111 in vb.
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 07:09 PM
#7
Thread Starter
Hyperactive Member
Ohhh
SORRY. My mistake. I was reading ion wrong way.
what a $#?@ i am! Thanks a lot. Also, how would i convert from a decimal to a binary, the same thing you gave me but vice-versa?
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 07:11 PM
#8
Thread Starter
Hyperactive Member
Sorry to ask so many questions but
So
iTemp += 1 << i;
would be iTemp = iTemp + 2^i
in VB?
Amon Ra
The Power of Learning.
-
Apr 8th, 2001, 05:38 AM
#9
Monday Morning Lunatic
That's correct
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 8th, 2001, 09:57 AM
#10
Thread Starter
Hyperactive Member
Cool
Thanks a lot. May I ask you one more thing?:
how would I write a function thst would now convert from decimal to a binary?
Amon Ra
The Power of Learning.
-
Apr 8th, 2001, 10:10 AM
#11
Monday Morning Lunatic
Code:
long Dec2Bin(long lNum, char *pcBuf) {
int i = 0;
while(lNum) {
if(lNum & 1) pcBuf[i] = '1';
else pcBuf[i] = '0';
lNum >>= 1;
i++;
}
pcBuf[i] = 0; // Null-terminated
return i;
}
void userfunc() {
char pcBuf[40];
Dec2Bin(27, pcBuf);
cout << pcBuf << endl;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 8th, 2001, 10:39 AM
#12
Thread Starter
Hyperactive Member
-
Apr 8th, 2001, 10:48 AM
#13
Monday Morning Lunatic
No problem
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 8th, 2001, 10:57 AM
#14
Thread Starter
Hyperactive Member
Sorry, one last thing :)
PortBits%(I) = PortNum% Mod 2
PortNum% = Fix(PortNum% / 2)
in c++? What would the Fix be?
Amon Ra
The Power of Learning.
-
Apr 8th, 2001, 11:08 AM
#15
Monday Morning Lunatic
Just cast it straight to an integral value to lose the fractional portion:
Code:
float fNum = 5.4f;
int iOther = (int)fNum;
cout << iOther << endl;
...produces "5"
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 8th, 2001, 11:10 AM
#16
Thread Starter
Hyperactive Member
Ok
Sorry if I annoy you, but for the dec2bin function, what should i change so that it display the "0" all the time.
eg: now if i put 3 it s gonna say 11
how would i change it so that it outputs 00000011?
Amon Ra
The Power of Learning.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|