- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Too much of a pain to work through for fun, but I particularly like the sequence of the first two comments. . .
Admin
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....
Admin
Glad to see that all those error conditions from yesterday's post are checked each time the function is called.
-ds
Admin
It's posts like these that truly demonstrate the truism that open source really attracts only and all the best programmers in the world.
Admin
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.
Admin
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.
Admin
Because... the sum of two eight-digits numbers is... a sixteen digit number, of course!
Admin
You need to be a politician.
Admin
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.
Admin
They must have been high. What's the point of this part?
Admin
I believe in VB that is equivalent to a string concatenation, i.e., h3 = dec2hex(d3) + h3
Admin
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.
Admin
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...
Admin
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'.
Admin
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.
Admin
Yes, they both do. In fact, is there any language that doesn't have this facility?
Admin
Show me an example then...
Admin
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.
Admin
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
Admin
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"...
Admin
I couldn't even continue reading halfway down. It was just.... terrible! Make the bad code stop :(
Admin
System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());
</FONT>prints 10 in the output window.
Admin
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) ;
Admin
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.
Admin
Excellent...I didn't know that NumberStyles.HexNumber existed...
Admin
This is a joke, right?
Admin
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>Admin
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.
Admin
Hum... maybe we all misunderstood, and he wants to add the digits, not their values...
dZ.
Admin
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>
Admin
Ooops - scratch that I forgot my MID function semantics for a minute....
Admin
<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>
Admin
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.
Admin
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.
Admin
Me too, pass the popcorn please :)
Admin
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
Admin
I think the joke is "System.Diagnostics.Debug.WriteLine" is a lot longer than "out <<"
and System.Globalization.NumberStyles.HexNumber
Admin
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()
Admin
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.
Admin
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>
Admin
LMAO!
Admin
Admin
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?
Admin
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!
Admin
does this shitty code work at all?
Admin
"The suspense is terrible, I hope it will last." - Willy Wonka (or Oscar Wilde, whichever you prefer).
Admin
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!
Admin
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.
Admin
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));
Admin
ASSHAT PEDANTIC SCRIPT: priceless