• Sam (unregistered)

    Too much of a pain to work through for fun, but I particularly like the sequence of the first two comments. . .

     

    Alex Papadimoulis:

      'Adds two 8-digit hexadecimals
    ...
      'longer one first
     

  • Foodie (unregistered)

    Why oh why won't someone invent a computer that operates in base 2?!?!?!  Then problems like this would be easy to solve.  Hmmm, I wonder if I could get a grant to investigate that....

  • (cs)

    Glad to see that all those error conditions from yesterday's post are checked each time the function is called.

    -ds

  • (cs)

    It's posts like these that truly demonstrate the truism that open source really attracts only and all the best programmers in the world.

  • arty (unregistered)

    Bravo!  This posting is particularly offensive.  I love the way the carry bit is carefully computed out of each nybble stage and then totally ignored.


  • NoName (unregistered) in reply to Foodie
    Anonymous:
    Why oh why won't someone invent a computer that operates in base 2?!?!?! Then problems like this would be easy to solve. Hmmm, I wonder if I could get a grant to investigate that....

    This base 2 thing is just so incredibly difficult, I doubt someone can come up with a base 2 computer. I mean, base 10 is the natural base, hex is 1.6 times better than base 10, but base 2 is five times worse than base 10. Five times worse means at least five times more complex. One-fifth of the population already has calculation problems with base ten, so 100% of the population has problems with base 2.

  • (cs)
    Alex Papadimoulis:

    Function AddHex(hex1, hex2)
    'Adds two 8-digit hexadecimals
    ...
     'trim / resize
    h3 = Right(h3,16)
    For i = Len(h3) to 15
    h3 = "0" & h3
    Next

    AddHex = h3

    End Function



    Because... the sum of two eight-digits numbers is... a sixteen digit number, of course!

  • (cs) in reply to NoName
    Anonymous:
    Anonymous:
    Why oh why won't someone invent a computer that operates in base 2?!?!?! Then problems like this would be easy to solve. Hmmm, I wonder if I could get a grant to investigate that....
    This base 2 thing is just so incredibly difficult, I doubt someone can come up with a base 2 computer. I mean, base 10 is the natural base, hex is 1.6 times better than base 10, but base 2 is five times worse than base 10. Five times worse means at least five times more complex. One-fifth of the population already has calculation problems with base ten, so 100% of the population has problems with base 2.


    You need to be a politician.
  • NoName (unregistered) in reply to rogthefrog
    rogthefrog:
    It's posts like these that truly demonstrate the truism that open source really attracts only and all the best programmers in the world.


    Real open source programmers don't touch BASIC with a ten foot pole. This work of art was for sure done by a script kidie posing as a programmer. Anyhow, I don't believe the open source storry. Who in his right mind buys open source? It is open source, you can have the source code for nothing... A company paying for this code doesn't deserve better, total lack of performing due diligence.
  • (cs)

    They must have been high. What's the point of this part?

    h3 = dec2hex(d3) & h3

  • Daveh (unregistered) in reply to whojoedaddy
    whojoedaddy:

    They must have been high. What's the point of this part?

    h3 = dec2hex(d3) & h3



    I believe in VB that is equivalent to a string concatenation, i.e., h3 = dec2hex(d3) + h3
  • (cs) in reply to NoName

    Anonymous:
    rogthefrog:
    It's posts like these that truly demonstrate the truism that open source really attracts only and all the best programmers in the world.


    Real open source programmers don't touch BASIC with a ten foot pole. This work of art was for sure done by a script kidie posing as a programmer. Anyhow, I don't believe the open source storry. Who in his right mind buys open source? It is open source, you can have the source code for nothing... A company paying for this code doesn't deserve better, total lack of performing due diligence.

    Well, open-source doesn't necessarily mean free.  You can sell products and provide the source to the public.  It's the license that's the key.

  • hmm (unregistered) in reply to dubwai

    Does VB6 or VB.net have any sort of functions/classes for converting hexadecimal strings to primitive types such as int?  Maybe this program allows the user to input hex strings into text boxes and thus the need for this crappy code.  I admit it's not the most brillant work, but only a true genus such as paula could produce code that is...

  • (cs) in reply to hmm

    Anonymous:
    Does VB6 or VB.net have any sort of functions/classes for converting hexadecimal strings to primitive types such as int?  Maybe this program allows the user to input hex strings into text boxes and thus the need for this crappy code.

    If it were the case that VB didn't have a way to parse strings with a hexidecimal radix then the appropriate action would be to create one.

    I think it's more likely that this person learned about hexidecimal but never really understood that they are just a different way to represent numbers.  They might even think that that computer does math 'in decimal'.

  • (cs) in reply to hmm
    Anonymous:
    Does VB6 or VB.net have any sort of functions/classes for converting hexadecimal strings to primitive types such as int?  Maybe this program allows the user to input hex strings into text boxes and thus the need for this crappy code.  I admit it's not the most brillant work, but only a true genus such as paula could produce code that is...

    Besides stuff like Cint("&H" + stringToConvert) (or Clng, or whatever type conversion you feel like using), no. And nobody in their right mind would use Hex$ to return a hex string -- too few lines, so programmer productivity appears to be low.

  • (cs) in reply to hmm

    Anonymous:
    Does VB6 or VB.net have any sort of functions/classes for converting hexadecimal strings to primitive types such as int?

    Yes, they both do.  In fact, is there any language that doesn't have this facility?

  • hmm (unregistered) in reply to JohnO
    JohnO:

    Anonymous:
    Does VB6 or VB.net have any sort of functions/classes for converting hexadecimal strings to primitive types such as int?

    Yes, they both do.  In fact, is there any language that doesn't have this facility?

    Show me an example then...

  • (cs)

    There are so many things wrong with this code that it's not even worth naming them.  It's just garbage, plain and simple.  The fact that someone paid for this makes me feel sick.

  • (cs) in reply to hmm

    Private Function AddHexCrapToLong(val1, val2) As Long
        Dim x As Long
        Dim y As Long
        
        x = CLng("&H" & val1)
        y = CLng("&H" & val2)
        
        AddHexCrapToLong = x + y
    End Function

    Private Function AddHexCrapToMoreHex(val1, val2) As Long
        AddHexCrapToMoreHex = Hex(AddHexCrapToLong(val1, val2))
    End Function

    Private Sub Command1_Click()
      MsgBox AddHexCrapToLong("AB", "CD")  --Result is 376 (base 10)
      MsgBox AddHexCrapToMoreHex("AB", "CD") --Result is 178 (base 16)
    End Sub

  • hmm (unregistered) in reply to christoofar

    christoofar:
    Private Function AddHexCrapToLong(val1, val2) As Long
        Dim x As Long
        Dim y As Long
        
        x = CLng("&H" & val1)
        y = CLng("&H" & val2)
        
        AddHexCrapToLong = x + y
    End Function

    Private Function AddHexCrapToMoreHex(val1, val2) As Long
        AddHexCrapToMoreHex = Hex(AddHexCrapToLong(val1, val2))
    End Function

    Private Sub Command1_Click()
      MsgBox AddHexCrapToLong("AB", "CD")  --Result is 376 (base 10)
      MsgBox AddHexCrapToMoreHex("AB", "CD") --Result is 178 (base 16)
    End Sub

    Ok I'm not familiar with VB at all...what does "&H" accomplish?  And how would this be accomplished in a language like C#? Because I'm not aware of any way to convert a hex string to int or long in that language.  Int32.Parse( string ) will throw an exception if you give it something like "F" or "0xF"...

  • (cs) in reply to dubwai

    I couldn't even continue reading halfway down. It was just.... terrible! Make the bad code stop :(

  • (cs) in reply to hmm
    Anonymous:
    JohnO:

    Anonymous:
    Does VB6 or VB.net have any sort of functions/classes for converting hexadecimal strings to primitive types such as int?

    Yes, they both do.  In fact, is there any language that doesn't have this facility?

    Show me an example then...

    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>

    prints 10 in the output window.

  • (cs) in reply to hmm
    Anonymous:

    christoofar:
    Private Function AddHexCrapToLong(val1, val2) As Long
        Dim x As Long
        Dim y As Long
        
        x = CLng("&H" & val1)
        y = CLng("&H" & val2)
        
        AddHexCrapToLong = x + y
    End Function

    Private Function AddHexCrapToMoreHex(val1, val2) As Long
        AddHexCrapToMoreHex = Hex(AddHexCrapToLong(val1, val2))
    End Function

    Private Sub Command1_Click()
      MsgBox AddHexCrapToLong("AB", "CD")  --Result is 376 (base 10)
      MsgBox AddHexCrapToMoreHex("AB", "CD") --Result is 178 (base 16)
    End Sub

    Ok I'm not familiar with VB at all...what does "&H" accomplish?  And how would this be accomplished in a language like C#? Because I'm not aware of any way to convert a hex string to int or long in that language.  Int32.Parse( string ) will throw an exception if you give it something like "F" or "0xF"...

    I believe you use the System.Convert.ToUInt32(<string>, <base>) to convert a string to an int32. For hex use 16 as the base like this: Number = System.Convert.ToUInt32("AB", 16) ;

     

  • (cs) in reply to hmm
    Anonymous:

    Ok I'm not familiar with VB at all...what does "&H" accomplish?  And how would this be accomplished in a language like C#? Because I'm not aware of any way to convert a hex string to int or long in that language.  Int32.Parse( string ) will throw an exception if you give it something like "F" or "0xF"...

    Apparently the '&H' prefix is what the CLng function recognizes as the format for a hex-decimal String.

    I'm going to wager that if you look at the API for Integer it's got another Parse method that takes a radix. That's the way it is in Java anyway.

  • hmm (unregistered) in reply to JohnO
    JohnO:
    Anonymous:
    JohnO:

    Anonymous:
    Does VB6 or VB.net have any sort of functions/classes for converting hexadecimal strings to primitive types such as int?

    Yes, they both do.  In fact, is there any language that doesn't have this facility?

    Show me an example then...

    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>

    prints 10 in the output window.

    Excellent...I didn't know that NumberStyles.HexNumber existed...

  • -L (unregistered) in reply to JohnO
    JohnO:
    <font size="2">

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </font>

    This is a joke, right?
  • John (unregistered)

    Ummm - are their hex numbers backwards?  Won't

    <FONT color=#000000>  </FONT>For<FONT color=#000000> i = Len(h1) </FONT>To<FONT color=#000000> 1 </FONT>Step<FONT color=#000000> -1
        d1 = hex2dec(Mid(h1,i,1))
        d2 = hex2dec(Mid(h2,i,1))
    </FONT>
    <FONT color=#000000></FONT>

    <FONT color=#000000>take the string one character at a time from the leftmost character?  Wouldn't that make the WTF the fact that the variable is called "carryover" when it should be called "carryunder"?

    </FONT>

     

  • (cs) in reply to -L
    Anonymous:
    JohnO:
    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>


    This is a joke, right?

     

    What's the joke?  You are using System.Diagnotics to write to the output  on your debugger (it writes strings, thus the ToString call).

    The output is a conversion of the Hex to an int (using the Parse method on the Int32 object).  This is pretty straightforward and easy enough to use.

    If this simple line of code is beyond you, please learn this phrase :  "Would you like fries with that?".  Or, you could just be an overpaid consultant and find your code posted here in the future.

  • (cs) in reply to Maurits
    Maurits:
    Alex Papadimoulis:

    Function AddHex(hex1, hex2)
    'Adds two 8-digit hexadecimals
    ...
     'trim / resize
    h3 = Right(h3,16)
    For i = Len(h3) to 15
    h3 = "0" & h3
    Next

    AddHex = h3

    End Function



    Because... the sum of two eight-digits numbers is... a sixteen digit number, of course!


    Hum... maybe we all misunderstood, and he wants to add the digits, not their values...

        dZ.

  • Perry Pederson (unregistered) in reply to hmm
    Anonymous:

    Ok I'm not familiar with VB at all...what does "&H" accomplish?  And how would this be accomplished in a language like C#? Because I'm not aware of any way to convert a hex string to int or long in that language.  Int32.Parse( string ) will throw an exception if you give it something like "F" or "0xF"...

    Straight from the Visual Studio 6.0 help documentation for VB 6.0:

    <FONT face=Arial>You can represent hexadecimal numbers directly by preceding numbers in the proper range with &H. For example, &H10 represents decimal 16 in hexadecimal notation.</FONT>

  • John (unregistered) in reply to John

    Ooops - scratch that I forgot my MID function semantics for a minute....    

  • Daniel T (unregistered) in reply to -L

    <font size="3">The relevant part is
    <font>Int32.Parse("A", System.Globalization.NumberStyles.HexNumber)
    </font>
    or
    <font>i</font><font>nt.Parse("A", System.Globalization.NumberStyles.HexNumber)

    The rest is just for output.

    (</font>
    </font><font size="3"><font>BTW, </font></font><font size="3"><font>is the second way is considered a WTF)?

    --Daniel T
    </font>
    </font>

  • (cs) in reply to John
    Anonymous:

    Ummm - are their hex numbers backwards?  Won't

    <FONT color=#000000>  </FONT>For<FONT color=#000000> i = Len(h1) </FONT>To<FONT color=#000000> 1 </FONT>Step<FONT color=#000000> -1
        d1 = hex2dec(Mid(h1,i,1))
        d2 = hex2dec(Mid(h2,i,1))
    </FONT>
    <FONT color=#000000></FONT>

    <FONT color=#000000>take the string one character at a time from the leftmost character?  Wouldn't that make the WTF the fact that the variable is called "carryover" when it should be called "carryunder"?

    </FONT>

     

    Nope, look at it again. If the len(h1) = 4 for example the mid function takes the the fourth character of the string. ie. FFFA  and returns the A.

    Not having tested the code myself nor the hex2dec and dec2hex functions, this should return the correct answer, question is WhyTF it was written at all when there are easier methods as shown above.

     

  • Lazy Lurker (unregistered) in reply to -L
    Anonymous:
    JohnO:
    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>


    This is a joke, right?

    Nice setup.  When someone says "It's not a joke, it's printing to the screen," will you start ranting about how much you think C# sucks, or will you smugly show a way to do it with 50% less code?

    Man, the suspense is killing me.

  • (cs) in reply to Lazy Lurker
    Anonymous:
    Anonymous:
    JohnO:
    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>


    This is a joke, right?

    Nice setup.  When someone says "It's not a joke, it's printing to the screen," will you start ranting about how much you think C# sucks, or will you smugly show a way to do it with 50% less code?

    Man, the suspense is killing me.

    Me too, pass the popcorn please :)

  • Daniel T (unregistered) in reply to Daniel T

    The sane way to do the Hex parsing, assuming the programmer either didn't know about the library functions or didn't want to use them for some reason, would be to evaluate each hex input as decimals and add those, as opposed to doing Hex math, digit by digit.  Which is insane.  "Don't forget to carry the B!"

    --Daniel T

  • dan (unregistered) in reply to scheky
    scheky:
    Anonymous:
    JohnO:
    <font size="2"> </font>

    <font size="2">System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());</font>


    This is a joke, right?

     

    What's the joke?  You are using System.Diagnotics to write to the output  on your debugger (it writes strings, thus the ToString call).

    The output is a conversion of the Hex to an int (using the Parse method on the Int32 object).  This is pretty straightforward and easy enough to use.

    If this simple line of code is beyond you, please learn this phrase :  "Would you like fries with that?".  Or, you could just be an overpaid consultant and find your code posted here in the future.


    I think the joke is "System.Diagnostics.Debug.WriteLine" is a lot longer than "out <<"
    and System.Globalization.NumberStyles.HexNumber

  • dan (unregistered) in reply to dan

    wierd it cut me off.

    I think the joke is "System.Diagnostics.Debug.WriteLine" is a lot longer than "out.write()"

    and System.Globalization.NumberStyles.HexNumber is a lot longer than str2hex()

  • (cs) in reply to dan

    The joke is that you guys seem to think hex isn't going to be base 16 in the future. Hell, maybe in ten years it'll be base 23, who knows, better use some crazy obscure system constant so we can recompile in any new base right away!

    It's not like you can change the constant if you switch to duodecimal or octal, you still have to change all the constant references. Oh man, using reflection to change System.Globalization.NumberStyles.HexNumber to 8 or 12 would be an awesome wtf candidate.

  • (cs) in reply to dan

    Anonymous:
    wierd it cut me off.

    I think the joke is "System.Diagnostics.Debug.WriteLine" is a lot longer than "out.write()"
    and System.Globalization.NumberStyles.HexNumber is a lot longer than str2hex()

    I included the namespaces so that the line of code would stand on it's own.  Console.Write() is equivalent to out.write() but I know those extra 4 characters are a killer when you can't touch type and you are using a crappy IDE that won't complete the names for you.<FONT size=2>

     

    </FONT>
  • Homer (unregistered) in reply to Daniel T

    Anonymous:
    The sane way to do the Hex parsing, assuming the programmer either didn't know about the library functions or didn't want to use them for some reason, would be to evaluate each hex input as decimals and add those, as opposed to doing Hex math, digit by digit.  Which is insane.  "Don't forget to carry the B!"

    --Daniel T

    LMAO!

  • (cs) in reply to foxyshadis
    foxyshadis:
    The joke is that you guys seem to think hex isn't going to be base 16 in the future. Hell, maybe in ten years it'll be base 23, who knows, better use some crazy obscure system constant so we can recompile in any new base right away!

    It's not like you can change the constant if you switch to duodecimal or octal, you still have to change all the constant references. Oh man, using reflection to change System.Globalization.NumberStyles.HexNumber to 8 or 12 would be an awesome wtf candidate.
    I think the parameter is meant to be thought of as a "NumberStyle" not a base. As if the converter doesn't do arbitrary conversions. It's possible (although, yeah, kinda silly) that the base 16 number style (aka, Hex) could be represented, internally, as number style 2 (or whatever).
  • Jeff R. (unregistered)

    So, the WTF is that he didn't trap the results of hex2dec for the '-1' error condition, but just adds it as if it were a normal number, right?

  • John (unregistered) in reply to Jeff R.
    So, the WTF is that he didn't trap the results of hex2dec for the '-1' error condition, but just adds it as if it were a normal number, right?

    Exactly. Looks like I was right about how the function was meant to be used (for extracting digit values to decimal, not for converting an entire hex number) ... and we were all right about terrible error handling.

    "I wrote something that generates an error on bad input ... and then I don't check for that error!"

    Brillant!

  • fmobus (unregistered) in reply to Jeff R.

    does this shitty code work at all?

  • (cs) in reply to res2
    res2:
    Anonymous:
    Anonymous:
    JohnO:
    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>


    This is a joke, right?

    Nice setup.  When someone says "It's not a joke, it's printing to the screen," will you start ranting about how much you think C# sucks, or will you smugly show a way to do it with 50% less code?

    Man, the suspense is killing me.

    Me too, pass the popcorn please :)

    "The suspense is terrible, I hope it will last." - Willy Wonka (or Oscar Wilde, whichever you prefer).

  • joe_bruin (unregistered)

    I think you're all missing the brilliance of this code: arbitrary precision arithmetic.  Sure, your machine can handle 32 and 64 and 128bit numbers, but what if you needed to multiple 4096bit numbers represented as hex.  your oh so simple VB equivalent of hex(hex2int(a) + hex2int(b)) is going to fail miserably, whereas this guy is only going to have to change a couple of numerical constants.

    BRILLANT!

  • grumble (unregistered)

    Heh!  I remember writing a complete library for hex string arithmetic for my high school's computer (a fancy new Apple ][+).  The algorithm looks right, but I can't believe VB doesn't have better ways to get this done.

     

  • heinzkunz (unregistered) in reply to Lazy Lurker
    Anonymous:
    Anonymous:
    JohnO:
    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>


    This is a joke, right?

    Nice setup.  When someone says "It's not a joke, it's printing to the screen," will you start ranting about how much you think C# sucks, or will you smugly show a way to do it with 50% less code?

    C#: 110 characters System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    Java: 44 characters System.out.print(Integer.parseInt("A", 16);

    BLOCKED SCRIPT 26 characters alert(parseInt("A", 16));

  • AC (unregistered) in reply to heinzkunz
    Anonymous:
    Anonymous:
    Anonymous:
    JohnO:
    <font size="2"> </font>

    <font size="2">System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());</font>


    This is a joke, right?

    Nice setup.  When someone says "It's not a joke, it's printing to the screen," will you start ranting about how much you think C# sucks, or will you smugly show a way to do it with 50% less code?

    C#: 110 characters System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    Java: 44 characters System.out.print(Integer.parseInt("A", 16);

    BLOCKED SCRIPT 26 characters alert(parseInt("A", 16));



    ASSHAT PEDANTIC SCRIPT: priceless


Leave a comment on “Hexing Around”

Log In or post as a guest

Replying to comment #:

« Return to Article