Results 1 to 11 of 11

Thread: vb to c++

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy

    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.

  2. #2
    Hyperactive Member Warmaster199's Avatar
    Join Date
    Aug 2000
    Location
    Canada
    Posts
    306
    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)

  3. #3

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy OK...

    Ok, thanks, basically what the first func. does is convert a decimal(integer) to a binary(string). The second one func converts a binary(string) to a decimal(integer).
    Could you please translate the functions to c++? I have never worked with strings in c++(mid, right...). I would appreciate if you could translate them, because I want to put them in a c++ dll. Thanks in advance
    Amon Ra
    The Power of Learning.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Thumbs up 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.

  6. #6

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question Also

    Should i make a char[8] to hold the binary string?
    Amon Ra
    The Power of Learning.

  7. #7

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy Last one :)

    How would i force the string to be only 8 char long?
    Amon Ra
    The Power of Learning.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  9. #9
    Hyperactive Member Warmaster199's Avatar
    Join Date
    Aug 2000
    Location
    Canada
    Posts
    306
    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)

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  11. #11

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Red face 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
  •  



Click Here to Expand Forum to Full Width