Results 1 to 32 of 32

Thread: Speed Challenge: Binary-Decimal!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270

    Exclamation Speed Challenge: Binary-Decimal!

    Here's a speed challenge: let's see who can code the fastest algorithms to convert longs to binary strings and vice versa. Must work with negative numbers.

    I'll post my code in a minute when I finish.

    If that was at all unclear, I mean something like convert 5 to 00000000000000000000000000000101. Your option whether or not to chop off leading zeros.
    Alphanos

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I wrote this awhile ago to get the numeric value of an 8-bit binary:

    VB Code:
    1. Private Sub Command1_Click()
    2. 'code removed
    3.  
    4. 'i worked too hard for it
    5. End Sub
    Last edited by The Hobo; Jan 13th, 2002 at 02:26 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    LOL

    Here's the code I wrote. If I make any improvements to it I'll post it.

    VB Code:
    1. Option Explicit
    2.     Dim Powers(0 To 31) As Long
    3.  
    4. Private Sub Form_Load()
    5.     Powers(0) = 1
    6.     Powers(1) = 2
    7.     Powers(2) = 4
    8.     Powers(3) = 8
    9.     Powers(4) = 16
    10.     Powers(5) = 32
    11.     Powers(6) = 64
    12.     Powers(7) = 128
    13.    
    14.     Powers(8) = 256
    15.     Powers(9) = 512
    16.     Powers(10) = 1024
    17.     Powers(11) = 2048
    18.     Powers(12) = 4096
    19.     Powers(13) = 8192
    20.     Powers(14) = 16384
    21.     Powers(15) = 32768
    22.    
    23.     Powers(16) = 65536
    24.     Powers(17) = 131072
    25.     Powers(18) = 262144
    26.     Powers(19) = 524288
    27.     Powers(20) = 1048576
    28.     Powers(21) = 2097152
    29.     Powers(22) = 4194304
    30.     Powers(23) = 8388608
    31.    
    32.     Powers(24) = 16777216
    33.     Powers(25) = 33554432
    34.     Powers(26) = 67108864
    35.     Powers(27) = 134217728
    36.     Powers(28) = 268435456
    37.     Powers(29) = 536870912
    38.     Powers(30) = 1073741824
    39.     Powers(31) = -2147483648#
    40.     Dim temptime As Double
    41.     Dim tempnum As String
    42.     Dim i As Long
    43.     temptime = Timer
    44.     For i = 1 To 7500: tempnum = DecToBin(-1): Next i
    45.     MsgBox Timer - temptime
    46. End Sub
    47.  
    48. Public Function BinToDec(ByRef TextString As String) As Long
    49.  
    50.     If Not CBool(Len(TextString)) Then Exit Function
    51.     Dim Bytes() As Byte
    52.     Dim Upper As Long
    53.     Dim i As Long
    54.  
    55.     Bytes = StrConv(StrReverse(TextString), vbFromUnicode)
    56.  
    57.     Upper = UBound(Bytes): If Upper > 31 Then Upper = 31
    58.  
    59.     Do
    60.         BinToDec = BinToDec Or (Powers(i) And CBool(Bytes(i) And 1))
    61.         i = i + 1
    62.     Loop Until i > Upper
    63.  
    64.     Erase Bytes
    65. End Function
    66.  
    67. Public Function DecToBin(ByVal LongVal As Long) As String
    68.     Dim Bytes(0 To 31) As Byte
    69.     Dim i As Long
    70.  
    71.     Do
    72.         If (LongVal And Powers(i)) Then Bytes(i) = 49 Else Bytes(i) = 48
    73.         i = i + 1
    74.     Loop Until i = 32
    75.  
    76.     DecToBin = StrConv(Bytes(), vbUnicode)
    77.     Erase Bytes
    78. End Function

    So write some conversion code and post it with a speed comparison.

    Edit: Code edited slightly to shave off a couple milliseconds.
    Last edited by Alphanos; Jan 13th, 2002 at 08:20 PM.
    Alphanos

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I whipped this up a couple minutes ago. Now I'm trimming it.
    Attached Files Attached Files
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    DaoK
    Guest
    VB Code:
    1. Private Function Binary(ascNumb As Integer) As String
    2. On Error Resume Next
    3.     Dim i As Integer
    4.     Binary = ""
    5.     For i = 7 To 0 Step -1
    6.         Binary = Binary & IIf(ascNumb And 2 ^ i, "1", "0")
    7.     Next i
    8. End Function
    9. Private Function BinToDec(binNumber As String) As Integer
    10. On Error Resume Next
    11.     Dim i As Integer
    12.     Dim SingleNumber As Integer
    13.     BinToDec = 0
    14.  
    15.     For i = Len(binNumber) - 1 To 0 Step -1
    16.         SingleNumber = Mid(binNumber, i + 1, 1)
    17.         BinToDec = BinToDec + (SingleNumber * 2 ^ (Len(binNumber) - 1 - i))
    18.     Next i
    19. End Function

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    here is a function to test, don't forget to include the time taken to load the array with powers.

    VB Code:
    1. Function Bin(ByVal X As Long) As String
    2.  If X < 2147483648# Then
    3.  
    4.     Bin = "0000000000000000000000000000000"
    5.        
    6.     If (X And 1) Then Mid(Bin, 31) = "1"
    7.     If (X And 2) Then Mid(Bin, 30) = "1"
    8.     If (X And 4) Then Mid(Bin, 29) = "1"
    9.     If (X And 8) Then Mid(Bin, 28) = "1"
    10.     If (X And 16) Then Mid(Bin, 27) = "1"
    11.     If (X And 32) Then Mid(Bin, 26) = "1"
    12.     If (X And 64) Then Mid(Bin, 25) = "1"
    13.     If (X And 128) Then Mid(Bin, 24) = "1"
    14.    
    15.     If (X And 256) Then Mid(Bin, 23) = "1"
    16.     If (X And 512) Then Mid(Bin, 22) = "1"
    17.     If (X And 1024) Then Mid(Bin, 21) = "1"
    18.     If (X And 2048) Then Mid(Bin, 20) = "1"
    19.     If (X And 4096) Then Mid(Bin, 19) = "1"
    20.     If (X And 8192) Then Mid(Bin, 18) = "1"
    21.     If (X And 16384) Then Mid(Bin, 17) = "1"
    22.     If (X And 32768) Then Mid(Bin, 16) = "1"
    23.    
    24.     If (X And 65536) Then Mid(Bin, 15) = "1"
    25.     If (X And 131072) Then Mid(Bin, 14) = "1"
    26.     If (X And 262144) Then Mid(Bin, 13) = "1"
    27.     If (X And 524288) Then Mid(Bin, 12) = "1"
    28.     If (X And 1048576) Then Mid(Bin, 11) = "1"
    29.     If (X And 2097152) Then Mid(Bin, 10) = "1"
    30.     If (X And 4194304) Then Mid(Bin, 9) = "1"
    31.     If (X And 8388608) Then Mid(Bin, 8) = "1"
    32.     '
    33.     If (X And 16777216) Then Mid(Bin, 7) = "1"
    34.     If (X And 33554432) Then Mid(Bin, 6) = "1"
    35.     If (X And 67108864) Then Mid(Bin, 5) = "1"
    36.     If (X And 134217728) Then Mid(Bin, 4) = "1"
    37.     If (X And 268435456) Then Mid(Bin, 3) = "1"
    38.     If (X And 536870912) Then Mid(Bin, 2) = "1"
    39.     If (X And 1073741824) Then Mid(Bin, 1) = "1"
    40.  End If
    41. End Function

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    I haven't tested The Hobo's yet, because he said he's working on it. Daok's is slower than mine, but Nucleus' function is about 3x faster. Lemme just do something to make it work with negatives....

    VB Code:
    1. Public Function DecToBin(ByVal X As Long) As String
    2.     DecToBin = "00000000000000000000000000000000"
    3.        
    4.     If (X And 1) Then Mid$(DecToBin, 32) = "1"
    5.     If (X And 2) Then Mid$(DecToBin, 31) = "1"
    6.     If (X And 4) Then Mid$(DecToBin, 30) = "1"
    7.     If (X And 8) Then Mid$(DecToBin, 29) = "1"
    8.     If (X And 16) Then Mid$(DecToBin, 28) = "1"
    9.     If (X And 32) Then Mid$(DecToBin, 27) = "1"
    10.     If (X And 64) Then Mid$(DecToBin, 26) = "1"
    11.     If (X And 128) Then Mid$(DecToBin, 25) = "1"
    12.    
    13.     If (X And 256) Then Mid$(DecToBin, 24) = "1"
    14.     If (X And 512) Then Mid$(DecToBin, 23) = "1"
    15.     If (X And 1024) Then Mid$(DecToBin, 22) = "1"
    16.     If (X And 2048) Then Mid$(DecToBin, 21) = "1"
    17.     If (X And 4096) Then Mid$(DecToBin, 20) = "1"
    18.     If (X And 8192) Then Mid$(DecToBin, 19) = "1"
    19.     If (X And 16384) Then Mid$(DecToBin, 18) = "1"
    20.     If (X And 32768) Then Mid$(DecToBin, 17) = "1"
    21.    
    22.     If (X And 65536) Then Mid$(DecToBin, 16) = "1"
    23.     If (X And 131072) Then Mid$(DecToBin, 15) = "1"
    24.     If (X And 262144) Then Mid$(DecToBin, 14) = "1"
    25.     If (X And 524288) Then Mid$(DecToBin, 13) = "1"
    26.     If (X And 1048576) Then Mid$(DecToBin, 12) = "1"
    27.     If (X And 2097152) Then Mid$(DecToBin, 11) = "1"
    28.     If (X And 4194304) Then Mid$(DecToBin, 10) = "1"
    29.     If (X And 8388608) Then Mid$(DecToBin, 9) = "1"
    30.    
    31.     If (X And 16777216) Then Mid$(DecToBin, 8) = "1"
    32.     If (X And 33554432) Then Mid$(DecToBin, 7) = "1"
    33.     If (X And 67108864) Then Mid$(DecToBin, 6) = "1"
    34.     If (X And 134217728) Then Mid$(DecToBin, 5) = "1"
    35.     If (X And 268435456) Then Mid$(DecToBin, 4) = "1"
    36.     If (X And 536870912) Then Mid$(DecToBin, 3) = "1"
    37.     If (X And 1073741824) Then Mid$(DecToBin, 2) = "1"
    38.     If (X And -2147483648#) Then Mid$(DecToBin, 1) = "1"
    39. End Function

    Also removed the if statement (it'd generate an overflow if it was greater than that before getting into the function anyway), and added the $ (which only makes it a very tiny bit faster).
    Alphanos

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    New binary to decimal function based on ideas from Nucleus' code, is much faster than my last one.


    VB Code:
    1. Public Function BinToDec(ByVal TextString As String) As Long
    2.     Dim Length As Long
    3.     Length = Len(TextString)
    4.     If Not CBool(Length) Then Exit Function
    5.  
    6.     If Length < 32 Then TextString = String(32 - Length, 48) & TextString
    7.     If Length > 32 Then TextString = Right$(TextString, 32)
    8.     Dim Bytes() As Byte
    9.     Bytes() = StrConv(TextString, vbFromUnicode)
    10.  
    11.     If Bytes(0) = 49 Then BinToDec = BinToDec Or -2147483648#
    12.     If Bytes(1) = 49 Then BinToDec = BinToDec Or 1073741824
    13.     If Bytes(2) = 49 Then BinToDec = BinToDec Or 536870912
    14.     If Bytes(3) = 49 Then BinToDec = BinToDec Or 268435456
    15.     If Bytes(4) = 49 Then BinToDec = BinToDec Or 134217728
    16.     If Bytes(5) = 49 Then BinToDec = BinToDec Or 67108864
    17.     If Bytes(6) = 49 Then BinToDec = BinToDec Or 33554432
    18.     If Bytes(7) = 49 Then BinToDec = BinToDec Or 16777216
    19.  
    20.     If Bytes(8) = 49 Then BinToDec = BinToDec Or 8388608
    21.     If Bytes(9) = 49 Then BinToDec = BinToDec Or 4194304
    22.     If Bytes(10) = 49 Then BinToDec = BinToDec Or 2097152
    23.     If Bytes(11) = 49 Then BinToDec = BinToDec Or 1048576
    24.     If Bytes(12) = 49 Then BinToDec = BinToDec Or 524288
    25.     If Bytes(13) = 49 Then BinToDec = BinToDec Or 262144
    26.     If Bytes(14) = 49 Then BinToDec = BinToDec Or 131072
    27.     If Bytes(15) = 49 Then BinToDec = BinToDec Or 65536
    28.  
    29.     If Bytes(16) = 49 Then BinToDec = BinToDec Or 32768
    30.     If Bytes(17) = 49 Then BinToDec = BinToDec Or 16384
    31.     If Bytes(18) = 49 Then BinToDec = BinToDec Or 8192
    32.     If Bytes(19) = 49 Then BinToDec = BinToDec Or 4096
    33.     If Bytes(20) = 49 Then BinToDec = BinToDec Or 2048
    34.     If Bytes(21) = 49 Then BinToDec = BinToDec Or 1024
    35.     If Bytes(22) = 49 Then BinToDec = BinToDec Or 512
    36.     If Bytes(23) = 49 Then BinToDec = BinToDec Or 256
    37.  
    38.     If Bytes(24) = 49 Then BinToDec = BinToDec Or 128
    39.     If Bytes(25) = 49 Then BinToDec = BinToDec Or 64
    40.     If Bytes(26) = 49 Then BinToDec = BinToDec Or 32
    41.     If Bytes(27) = 49 Then BinToDec = BinToDec Or 16
    42.     If Bytes(28) = 49 Then BinToDec = BinToDec Or 8
    43.     If Bytes(29) = 49 Then BinToDec = BinToDec Or 4
    44.     If Bytes(30) = 49 Then BinToDec = BinToDec Or 2
    45.     If Bytes(31) = 49 Then BinToDec = BinToDec Or 1
    46.  
    47.     TextString = vbNullString
    48.     Erase Bytes
    49. End Function

    Unless someone can come up with something faster(which is unlikely) it looks like Nucleus is the winner.
    Alphanos

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    I'm pretty sure that Nucleus' decimal to binary function is as fast as you can get; can anyone speed up the binary to decimal function I wrote from ideas from his code?

    Time to start another thread: speed challenge for four functions: to and from hex with binary and decimal.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270

  11. #11
    DaoK
    Guest
    Daok's is slower than mine
    well mine take 5 lines heyhey... and personally I do not see why it should be more bigger than that.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    No offense intended, sorry if I caused any. Its just that this is a speed challenge; your code is shorter, but requires more time to do the conversion.

  13. #13
    DaoK
    Guest
    How many time more fast is the HUGE code ? Because I do not think that you have all possibility with that HUGE code ?

  14. #14
    Tygur
    Guest
    Originally posted by DaoK
    How many time more fast is the HUGE code ? Because I do not think that you have all possibility with that HUGE code ?
    If a code takes up more lines, it doesn't necessarily mean it's slower. The amount of lines is a very poor indication of speed.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    *Agrees with Tygur*

    Nucleus' decimal to binary conversion code runs about 7.5 times faster than yours on my computer.

  16. #16
    Tygur
    Guest
    Daok didn't even have a chance. His code uses the very slow IIf function, and it lengthens a string in a loop. It's not really written with speed in mind. Also, it takes an integer and only processes a byte. Alphanos called for code that processes a Long.

  17. #17
    New Member
    Join Date
    Jan 2002
    Posts
    4
    #include <iostream>
    #include <string>

    using namespace std;

    double base2, base8, base10, base16, baseIn, baseConvert;
    char bitChar;
    int bitValue, bit, byteValue, base81, base82, base83;
    int baseOfTwo[8]={1,2,4,8,16,32,64,128};
    int baseofEight[8]={1,2,4,1,2,4,1,2};
    int baseOfTen[8]={0,1,2,3,4,5,6,7};
    int baseOfSixteen[8]={1,2,4,8,1,2,4,8};






    void base2conv()
    {
    cout << "Enter an 8 bit value to convert: " << endl;

    for (bit=7; bit >=0; bit --)
    {
    cin >> bitChar;
    bitValue=bitChar - '0';
    }

    for (bit=0; bit <=2; bit++)
    {
    if (bitValue==1)
    base81+=baseOfEight[bit];
    }

    for (bit=3; bit <=5; bit++)
    {
    if (bitValue==1)
    base82+=baseOfEight[bit];
    }

    for (bit=6; bit <=7; bit++)
    {
    if (bitValue==1)
    base83+=baseOfEight[bit];
    }

    cout << "The base 8 value is: " << base83 << base82 << base81 ;

    }



    cout << "What base would you like to convert to (8, 10, 16)?" << endl;
    cin >> baseConvert;

    if (baseConvert == 8)
    {








    int main()
    {
    cout << "Enter a base to be converted: " << endl;
    cin >> baseIn;


    if (baseIn == 2)
    {
    base2conv();
    }

    else if (baseIn == 8)
    {
    base8conv();
    }

    else if (baseIn == 10)
    {
    base10conv();
    }

    else if (baseIn ==16)
    {
    base16conv();
    }

    else
    {
    cout << "You have entered an invalid base. "

    return 1;
    }


    hehe, much easier in c++

  18. #18
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    bah!

    i had to write something like this for my C++ class, convert a binary number into a decimal or vice versa, the whole while accepting the inputs as strings. (We had to write our own string to binary/binary to string conversion class.)

    Let me tell ya, it wasn't fun then, and it sure as hell wouldn't be fun now.

  19. #19
    New Member
    Join Date
    Jan 2002
    Posts
    4
    lol, I know what u mean, whered u think i got that from, pulled it from the closet

    u probably did something like this....//oh boy I'm gonna be hated for this post

    #include <iostream.h>

    int baseOfEight[8]={1,2,4,1,2,4,1,2};
    int baseOfTen[8]={1,2,4,8,16,32,64,128};
    int baseOfSixteen[8]={1,2,4,8,1,2,4,8};

    //base2 convert to base8
    void base2conv8()
    {
    char bitChar;
    int bitValue, bit;
    int base81=0, base82=0, base83=0;
    cout << "Enter an 8 bit value in base 2 form to convert: " << endl;

    for (bit=7; bit >=6; bit --) // first 2 bits
    {
    cin >> bitChar;
    bitValue=bitChar - '0';
    if (bitValue==1)
    base83+=baseOfEight[bit];
    }

    for (bit=5; bit >=3; bit--) // next 3 bits
    {
    cin >> bitChar;
    bitValue=bitChar -'0';
    if (bitValue==1)
    base82+=baseOfEight[bit];
    }

    for (bit=2; bit >=0; bit--) // last 3 bits
    {
    cin >> bitChar;
    bitValue=bitChar -'0';
    if (bitValue==1)
    base81+=baseOfEight[bit];
    }

    cout << "The base 8 value is: " << base83 << base82 << base81 ;

    }

    //base2 convert to base10
    void base2conv10()
    {
    char bitChar=0;
    int bitValue=0, bit=0, byteValue=0;

    cout << "Enter an 8 bit value in base 2 form to convert: " << endl;

    for (bit=7; bit >=0; bit --) // counts all the bits and multiplies the
    { // ones with value 1 with corresponding value
    cin >> bitChar; // in baseOfTen function and then sums them
    bitValue=bitChar - '0'; // in the variable byteValue
    if (bitValue==1)
    byteValue+=baseOfTen[bit];
    }

    cout << "The base 10 value is: " << byteValue << endl;

    }

    //base2 convert to base16
    void base2conv16()
    {
    int base161=0, base162=0;
    char bitChar;
    int bitValue, bit;

    cout << "Enter an 8 bit value in base 2 form to convert: " << endl;


    for (bit=7; bit >=4; bit--) // Reads first 4 bits
    {
    cin >> bitChar;
    bitValue=bitChar -'0'; // Converts to 0 or 1
    if (bitValue==1) // if true
    base162+=baseOfSixteen[bit]; //adds the sum of 1*baseOfSixteen[bit[

    switch (base162) // Converts values >= 10 to correct hex value
    {
    case 10: base162='A'; break;
    case 11: base162='B'; break;
    case 12: base162='C'; break;
    case 13: base162='D'; break;
    case 14: base162='E'; break;
    case 15: base162='F'; break;

    }

    }

    for (bit=3; bit >=0; bit--)
    {
    cin >> bitChar;
    bitValue=bitChar -'0';
    if (bitValue==1)
    base161+=baseOfSixteen[bit];

    switch (base161)
    {
    case 10: base161='A'; break;
    case 11: base161='B'; break;
    case 12: base161='C'; break;
    case 13: base161='D'; break;
    case 14: base161='E'; break;
    case 15: base161='F'; break;
    }
    }
    if ((base162>9) && (base161>9)) //Test to see if type casting necessary
    {
    cout << "The base 16 value is: " << char(base162) << char(base161) << endl;
    }

    else if ((base162>9) && (base161<=9))
    {
    cout << "The base 16 value is: " << char(base162) << base161 << endl;
    }

    else if ((base162<=9) && (base161>9))
    {
    cout << "The base 16 value is: " << base162 << char(base161) << endl;
    }

    else
    cout << "The base 16 value is: " << base162 << base161 << endl;
    }


    //base8 convert to base2; 8 to 2 to 10; 8 to 2 to 16
    int base8conv2(int& test)
    {

    int base161=0, base162=0;
    int bit=0, temp=0, bitValue=0, byteValue=0;
    int base8num[3]={0,0,0}, base2num[8]={0,0,0,0,0,0,0,0};


    cout << "Enter an 8 bit value in base 8 form to convert: " << endl;
    for (bit=0; bit <=2; bit++)
    {
    cin >> base8num[bit];

    }
    //Tests all octal values
    if (base8num[0]>3 || base8num[1]>7 || base8num[2]>7)
    {
    cout << "One of the values you entered was and invalid 8 bit base 8 value!" << endl;
    cout << "Please quit and try again." << endl << endl;
    return 0;
    }
    //base8 first 2 bits
    base2num[7]=base8num[0]/2;
    temp=base8num[0]%2;
    base8num[0]=temp;
    base2num[6]=base8num[0]/1;

    //base8 next 3 bits
    temp=0;
    base2num[5]=base8num[1]/4;
    temp=base8num[1]%4;
    base8num[1]=temp;
    base2num[4]=base8num[1]/2;
    temp=base8num[1]%2;
    base8num[1]=temp;
    base2num[3]=base8num[1]/1;

    //base8 last 3 bits
    temp=0;
    base2num[2]=base8num[2]/4;
    temp=base8num[2]%4;
    base8num[2]=temp;
    base2num[1]=base8num[0]/2;
    temp=base8num[2]%2;
    base8num[2]=temp;
    base2num[0]=base8num[2]/1;

    if (test==0) //for conversion of 8 to 2
    {
    cout << base2num[7] << base2num[6] << base2num[5] << base2num[4] << base2num[3]
    << base2num[2] << base2num[1] << base2num[0] << endl;
    return 0;
    }

    else if (test==1) //Convert now 8 to 2 to 10
    {
    for (bit=7; bit>=0; bit--)
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    byteValue+=baseOfTen[bit];
    }

    cout << "The base 10 value is: " << byteValue << endl;
    return 0;
    }
    else if (test==2) //Convert now 8 to 2 to 16
    {
    for (bit=7; bit >=4; bit--) // Reads first 4 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1) // if true
    base162+=baseOfSixteen[bit]; //adds the sum of 1*baseOfSixteen[bit[

    switch (base162) // Converts values >= 10 to correct hex value
    {
    case 10: base162='A'; break;
    case 11: base162='B'; break;
    case 12: base162='C'; break;
    case 13: base162='D'; break;
    case 14: base162='E'; break;
    case 15: base162='F'; break;
    }
    }

    }

    for (bit=3; bit >=0; bit--)
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base161+=baseOfSixteen[bit];

    switch (base161)
    {
    case 10: base161='A'; break;
    case 11: base161='B'; break;
    case 12: base161='C'; break;
    case 13: base161='D'; break;
    case 14: base161='E'; break;
    case 15: base161='F'; break;
    }
    }
    if ((base162>9) && (base161>9)) //Test to see if type casting necessary
    {
    cout << "The base 16 value is: " << char(base162) << char(base161) << endl;
    }

    else if ((base162>9) && (base161<=9))
    {
    cout << "The base 16 value is: " << char(base162) << base161 << endl;
    }

    else if ((base162<=9) && (base161>9))
    {
    cout << "The base 16 value is: " << base162 << char(base161) << endl;
    }

    else
    cout << "The base 16 value is: " << base162 << base161 << endl;

    return 0;
    }

    //base10 convert to 2; base10 to 2 to 8; 10 to 2 to 16
    int base10conv2(int& test)
    {
    int base161=0, base162=0;
    int bit=0, temp=0, bitValue=0, byteValue=0;
    int base2num[8]={0,0,0,0,0,0,0,0}, base10num;
    int base81=0, base82=0, base83=0;


    cout << "Enter an 8 bit value in base 10 form to convert: " << endl;
    cin >> base10num;
    //test for positive value
    if (base10num<0)
    {
    cout<< "Base 10 value must be a positive value!" << endl;
    return 0;
    }
    base2num[7]=base10num/128; //begin of base10 to 2 convert
    temp=base10num%128;
    base10num=temp;
    base2num[6]=base10num/64;
    temp=base10num%64;
    base10num=temp;
    base2num[5]=base10num/32;
    temp=base10num%32;
    base10num=temp;
    base2num[4]=base10num/16;
    temp=base10num%16;
    base10num=temp;
    base2num[3]=base10num/8;
    temp=base10num%8;
    base10num=temp;
    base2num[2]=base10num/4;
    temp=base10num%4;
    base10num=temp;
    base2num[1]=base10num/2;
    temp=base10num%2;
    base10num=temp;
    base2num[0]=base10num/1;

    if (test==0) //for conversion of 10 to 2
    {
    cout << base2num[7] << base2num[6] << base2num[5] << base2num[4] << base2num[3]
    << base2num[2] << base2num[1] << base2num[0] << endl;
    return 0;
    }

    else if (test==1) //Convert now 10 to 2 to 8
    {
    for (bit=7; bit >=6; bit --) // first 2 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base83+=baseOfEight[bit];
    }

    for (bit=5; bit >=3; bit--) // next 3 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base82+=baseOfEight[bit];
    }

    for (bit=2; bit >=0; bit--) // last 3 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base81+=baseOfEight[bit];
    }
    cout << "The base 8 value is: " << base83 << base82 << base81 ;
    return 0;
    }

    else if (test==2) //Convert now 10 from 2 to 16
    {
    for (bit=7; bit >=4; bit--) // Reads first 4 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1) // if true
    base162+=baseOfSixteen[bit]; //adds the sum of 1*baseOfSixteen[bit[

    switch (base162) // Converts values >= 10 to correct hex value
    {
    case 10: base162='A'; break;
    case 11: base162='B'; break;
    case 12: base162='C'; break;
    case 13: base162='D'; break;
    case 14: base162='E'; break;
    case 15: base162='F'; break;
    }
    }

    }

    for (bit=3; bit >=0; bit--)
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base161+=baseOfSixteen[bit];

    switch (base161)
    {
    case 10: base161='A'; break;
    case 11: base161='B'; break;
    case 12: base161='C'; break;
    case 13: base161='D'; break;
    case 14: base161='E'; break;
    case 15: base161='F'; break;
    }
    }
    if ((base162>9) && (base161>9)) //Test to see if type casting necessary
    {
    cout << "The base 16 value is: " << char(base162) << char(base161) << endl;
    }

    else if ((base162>9) && (base161<=9))
    {
    cout << "The base 16 value is: " << char(base162) << base161 << endl;
    }

    else if ((base162<=9) && (base161>9))
    {
    cout << "The base 16 value is: " << base162 << char(base161) << endl;
    }

    else
    cout << "The base 16 value is: " << base162 << base161 << endl;

    return 0;
    }
    Last edited by VBnewbC++er; Jan 14th, 2002 at 12:34 AM.

  20. #20
    New Member
    Join Date
    Jan 2002
    Posts
    4
    here is rest

    //base16 convert to 2; base16 to 2 to 8; 10 to 2 to 10
    int base16conv2(int& test)
    {
    char base161=0, base162=0;
    int bit=0, temp=0, bitValue=0, byteValue=0;
    int base2num[8]={0,0,0,0,0,0,0,0};
    int base81=0, base82=0, base83=0;

    cout << "Enter an 8 bit value in base 16 form to convert: " << endl;
    cin >> base162 >> base161;

    switch (base162) //assigns 4 binary values corresponding to hex value
    {
    case '0': base2num[7]=0; base2num[6]=0; base2num[5]=0; base2num[4]=0; break;
    case '1': base2num[7]=0; base2num[6]=0; base2num[5]=0; base2num[4]=1; break;
    case '2': base2num[7]=0; base2num[6]=0; base2num[5]=1; base2num[4]=0; break;
    case '3': base2num[7]=0; base2num[6]=0; base2num[5]=1; base2num[4]=1; break;
    case '4': base2num[7]=0; base2num[6]=1; base2num[5]=0; base2num[4]=0; break;
    case '5': base2num[7]=0; base2num[6]=1; base2num[5]=0; base2num[4]=1; break;
    case '6': base2num[7]=0; base2num[6]=1; base2num[5]=1; base2num[4]=0; break;
    case '7': base2num[7]=0; base2num[6]=1; base2num[5]=1; base2num[4]=1; break;
    case '8': base2num[7]=1; base2num[6]=0; base2num[5]=0; base2num[4]=0; break;
    case '9': base2num[7]=1; base2num[6]=0; base2num[5]=0; base2num[4]=1; break;
    case 'A': base2num[7]=1; base2num[6]=0; base2num[5]=1; base2num[4]=0; break;
    case 'B': base2num[7]=1; base2num[6]=0; base2num[5]=1; base2num[4]=1; break;
    case 'C': base2num[7]=1; base2num[6]=1; base2num[5]=0; base2num[4]=0; break;
    case 'D': base2num[7]=1; base2num[6]=1; base2num[5]=0; base2num[4]=1; break;
    case 'E': base2num[7]=1; base2num[6]=1; base2num[5]=1; base2num[4]=0; break;
    case 'F': base2num[7]=1; base2num[6]=1; base2num[5]=1; base2num[4]=1; break;
    }

    switch (base161)
    {
    case '0': base2num[3]=0; base2num[2]=0; base2num[1]=0; base2num[0]=0; break;
    case '1': base2num[3]=0; base2num[2]=0; base2num[1]=0; base2num[0]=1; break;
    case '2': base2num[3]=0; base2num[2]=0; base2num[1]=1; base2num[0]=0; break;
    case '3': base2num[3]=0; base2num[2]=0; base2num[1]=1; base2num[0]=1; break;
    case '4': base2num[3]=0; base2num[2]=1; base2num[1]=0; base2num[0]=0; break;
    case '5': base2num[3]=0; base2num[2]=1; base2num[1]=0; base2num[0]=1; break;
    case '6': base2num[3]=0; base2num[2]=1; base2num[1]=1; base2num[0]=0; break;
    case '7': base2num[3]=0; base2num[2]=1; base2num[1]=1; base2num[0]=1; break;
    case '8': base2num[3]=1; base2num[2]=0; base2num[1]=0; base2num[0]=0; break;
    case '9': base2num[3]=1; base2num[2]=0; base2num[1]=0; base2num[0]=1; break;
    case 'A': base2num[3]=1; base2num[2]=0; base2num[1]=1; base2num[0]=0; break;
    case 'B': base2num[3]=1; base2num[2]=0; base2num[1]=1; base2num[0]=1; break;
    case 'C': base2num[3]=1; base2num[2]=1; base2num[1]=0; base2num[0]=0; break;
    case 'D': base2num[3]=1; base2num[2]=1; base2num[1]=0; base2num[0]=1; break;
    case 'E': base2num[3]=1; base2num[2]=1; base2num[1]=1; base2num[0]=0; break;
    case 'F': base2num[3]=1; base2num[2]=1; base2num[1]=1; base2num[0]=1; break;
    }

    if (test==0) //for conversion of 16 to 2
    {
    cout << "The base 2 form is: ";
    cout << base2num[7] << base2num[6] << base2num[5] << base2num[4] << base2num[3]
    << base2num[2] << base2num[1] << base2num[0] << endl;
    return 0;
    }

    else if (test==1) //Convert now 16 from 2 to 8
    {
    for (bit=7; bit >=6; bit --) // first 2 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base83+=baseOfEight[bit];
    }

    for (bit=5; bit >=3; bit--) // next 3 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base82+=baseOfEight[bit];
    }

    for (bit=2; bit >=0; bit--) // last 3 bits
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    base81+=baseOfEight[bit];
    }
    cout << "The base 8 value is: " << base83 << base82 << base81 ;
    return 0;
    }

    else if (test==2) //Convert now 16 from 2 to 10
    {
    for (bit=7; bit>=0; bit--)
    {
    bitValue=base2num[bit];
    if (bitValue==1)
    byteValue+=baseOfTen[bit];
    }

    cout << "The base 10 value is: " << byteValue << endl;
    return 0;
    }

    return 0;
    }

    int main()
    {
    int baseIn, baseConv;

    cout << "What base number do you wish to use? (2, 8, 10, 16)" << endl;
    cin >> baseIn;
    if (baseIn==2)
    {
    cout << "What base do you want to convert to? (8, 10, 16)" << endl;
    cin >> baseConv;
    if (baseConv==8)
    {
    base2conv8();
    }
    else if(baseConv==10)
    {
    base2conv10();
    }
    else if(baseConv==16)
    {
    base2conv16();
    }
    else
    {
    cout << "You entered an invalid base to convert to!" << endl;
    return 0;
    }

    }
    else if (baseIn==8)
    {
    cout << "What base do you want to convert to? (2, 10, 16)" << endl;
    cin >> baseConv;
    if (baseConv==2)
    {
    int test=0;
    base8conv2(test);
    }
    else if(baseConv==10)
    {
    int test=1;
    base8conv2(test);

    }
    else if(baseConv==16)
    {
    int test=2;
    base8conv2(test);
    }
    else
    {
    cout << "You entered an invalid base to covert to!" << endl;
    return 0;
    }
    }
    else if (baseIn==10)
    {
    cout << "What base do you want to convert to? (2, 8, 16)" << endl;
    cin >> baseConv;
    if (baseConv==2)
    {
    int test=0;
    base10conv2(test);
    }
    else if(baseConv==8)
    {
    int test=1;
    base10conv2(test);
    }
    else if(baseConv==16)
    {
    int test=2;
    base10conv2(test);
    }
    else
    {
    cout << "You entered an invalid base to covert to!" << endl;
    return 0;
    }
    }
    else if (baseIn==16)
    {
    cout << "What base do you want to convert to? (2, 8, 10)" << endl;
    cin >> baseConv;
    if (baseConv==2)
    {
    int test=0;
    base16conv2(test);
    }
    else if(baseConv==8)
    {
    int test=1;
    base16conv2(test);
    }
    else if(baseConv==10)
    {
    int test=2;
    base16conv2(test);
    }
    else
    {
    cout << "You entered an invalid base to covert to!" << endl;
    return 0;
    }
    }
    else
    {
    cout << "You have entered an in valid base!" << endl;
    return 0;
    }

    return 0;

    }is right..............

  21. #21
    New Member
    Join Date
    Jan 2002
    Posts
    4
    whewww, my great instructor gave us a whole week for that 2, lol sry bout the C++guys, I just picked up my first VB book today and ripped a version of Rose to play with

  22. #22
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yeah, we were given a week also...
    Debugging it was hell too... i think i lost 4 points for poor comments in the code (hell i was lucky I got it submitted by the deadline)

    I'll post my code if i can find it but im pretty sure I erased that %#$% as soon as the grade was in....

  23. #23
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    If that C++ code was written well, im sure it will run faster than anything equivalent in VB. But the challenge was in VB...
    You just proved that sig advertisements work.

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    Since its two in the morning, I'm sure you'll forgive me if I try testing out C++ code sometime later. Just putting the files together and compiling them would take several minutes, and I have spent much less time on C++ than I have on VB, so I don't know how to make a timer to test the time it takes for a couple thousand iterations.

    In a couple days though.
    Alphanos

  25. #25
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    What is an acceptable negative?

    i.e - What is the binary of -123 ?

    Anyway, here's my best effort. With 5 minutes in it, I can't brag. Just curious how fast it is (not very methinks...)

    Code:
    Private Function CBin(IntVal As Long)
        Dim MaxPower As Integer
        Dim i As Integer
        Dim BinaryString As String
        
        Do Until 2 ^ MaxPower >= IntVal
            MaxPower = MaxPower + 1
        Loop
        
        Do Until MaxPower < 0
            If (IntVal - (2 ^ MaxPower)) >= 0 Then
                BinaryString = BinaryString & "1"
                IntVal = IntVal - 2 ^ MaxPower
            Else
                BinaryString = BinaryString & "0"
            End If
            MaxPower = MaxPower - 1
        Loop
        
        CBin = BinaryString
        
    End Function
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  26. #26
    Tygur
    Guest
    A negative number is indicated by whether a certain bit is set. I forget whether this bit is the rightmost or leftmost bit.

  27. #27
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by Tygur
    A negative number is indicated by whether a certain bit is set. I forget whether this bit is the rightmost or leftmost bit.
    Left, which is pretty logical if u think about it.. ie u still need to perform logical comparisons up to the last bit.
    Regards
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  28. #28
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Figured as much. Oh well, can't be bothered adding that ability - it's just a matter of changing a - to a + and modifying the appropriate bit.

    And, here's my brother function, Bin to Dec:

    Code:
    Private Function ToDec(BinString As String)
        Dim DecNum As Long
        Dim i As Integer, j As Integer
        
        j = Len(BinString)
        
        For i = 1 To j
            If Mid(BinString, i, 1) = "1" Then
                DecNum = DecNum + 2 ^ (j - i)
            End If
        Next i
        
        ToDec = DecNum
    End Function
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  29. #29
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by rjlohan
    Figured as much. Oh well, can't be bothered adding that ability - it's just a matter of changing a - to a + and modifying the appropriate bit.
    It would be lovely t'were it as easy as that. you cant just make a number negative by switching the leftmost bit... I posted something recently on signed integers to binary and back again.
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  30. #30
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    From what I understand (or seem to remember, amidst a beer-haze) a negative-bit binary number operates on 7-bits per byte, where the 8th (leftmost) bit is +/- depending. Where did I go wrong?


    (Oh that's right, I skipped lectures to go drink beer and watch Fight Club...)
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    I'm in front of the computer.
    Posts
    270
    To change the sign of a number, NOT it and add one.
    IE. 285

    0000000100011101 (integer) = 285
    1111111011100011 (integer) = -285
    Alphanos

  32. #32
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    I wouldn't mind getting my "dec to binary" code evaluated, although its more of a "String to Binary" process.
    The Key Sub is Command1_Click, or:
    VB Code:
    1. Private Sub Command1_Click()
    2. If Len(Text1.Text) = 0 Then
    3.     MsgBox "Sorry, Please enter some text to convert first!"
    4.     Exit Sub
    5. End If
    6. Dim MyStr As String
    7. Dim i As Long
    8. For i = 1 To Len(Text1.Text)
    9.     MyStr = MyStr & ARR_BIN(Asc(Mid(Text1.Text, i, 1)))
    10. Next i
    11. Text3.Text = MyStr
    12. End Sub

    The setup was done in Form_Load{I think} but once done, this thig is Speedy.

    -Lou
    PS:
    'Course, its not converting Dec Numbers to Bin Numbers, So I guess, Ignore it. This is the wrong thread. But its the closest I've seen so far.
    Attached Files Attached Files

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