|
-
Apr 7th, 2001, 11:50 AM
#1
Thread Starter
Hyperactive Member
HOW WOULD THIS FUNCTION BE WRITTEN IN C++:
Private Function CBinary(ByVal dec as integer) as String
dim x as integer
CBinary = ""
For x = 0 to 7
if dec and 2^x then
CBinary = "1" & CBinary
else
CBinary = "0" & CBinary
end if
Next x
End Function
WHAT ABOUT THIS ONE:
Private Function CDecimal(ByVal bin as String) as Integer
dim x as Integer
for x=0 to (len(bin)-1)
If Mid(bin, Len(bin) - x, 1) = "1" Then
CDecimal = CDecimal + (2^x)
End If
Next x
End Function
How would i convert these vb functions to correct c++ functions? Thanx in advance.
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 12:13 PM
#2
Hyperactive Member
The second one looks like it's supposed to convert a string to an integer...
This will convert an int to a string:
char *itoa(int value, char *digits, int base);
long to string:
char *ltoa(long value, char *digits, int base);
string to int:
int atoi(char *string);
string to long:
long atol(char *string);
Designer/Programmer of the Comtech Operating System(CTOS)
-
Apr 7th, 2001, 12:37 PM
#3
Thread Starter
Hyperactive Member
-
Apr 7th, 2001, 12:43 PM
#4
Monday Morning Lunatic
Use itoa and atoi, but set the base (or radix) to 2.
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, 12:50 PM
#5
Thread Starter
Hyperactive Member
Hmm
Is that how to convert decimal to binary in c++? If yes it is not to hard BTW, do I need to include a header for the string functions?
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 12:53 PM
#6
Thread Starter
Hyperactive Member
Also
Should i make a char[8] to hold the binary string?
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 12:58 PM
#7
Thread Starter
Hyperactive Member
Last one :)
How would i force the string to be only 8 char long?
Amon Ra
The Power of Learning.
-
Apr 7th, 2001, 02:13 PM
#8
Monday Morning Lunatic
If you cast it to char then it will be no more than 8 chars. However, bear in mind that if you supply a negative number it will have the 8th bit as 1 (the sign bit).
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, 02:22 PM
#9
Hyperactive Member
For strings in C/C++, do a
#include<string.h>
For those itoa() and atoi(), do a
#include<stdlib.h>
To declare a string, do this:
char *var[]; or
char *var;
To do a fixed-length string of say 20 chars:
char *var[20];
And there you have it... Strings in C/C++
Designer/Programmer of the Comtech Operating System(CTOS)
-
Apr 7th, 2001, 02:25 PM
#10
Monday Morning Lunatic
To do a fixed-length string of say 20 chars:
char *var[20];
It's char var[20];
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, 02:47 PM
#11
Thread Starter
Hyperactive Member
Cool
Ok ,thanks a lot, i am gonna try that. Thx
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
|