Comment On Hexing Around

Yesterday's post was surprisingly difficult to compose. Here I was, presented with all of the code from an "open source" system that Taka's company purchased, and completely unable to compress its quality into the short bit we're all used to seeing. I decided to limit myself to just the encryption module, but even that was too saturated for a single post. Here's another function I came across from the code that I felt deserved its own post ... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Hexing Around

2005-08-23 15:00 • by Sam

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
 

Re: Hexing Around

2005-08-23 15:02 • by Foodie
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....

Re: Hexing Around

2005-08-23 15:04 • by DisturbedSaint
Glad to see that all those error conditions from yesterday's post are checked each time the function is called.



-ds

Re: Hexing Around

2005-08-23 15:05 • by 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.

Re: Hexing Around

2005-08-23 15:10 • by arty
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.





Re: Hexing Around

2005-08-23 15:10 • by NoName
41528 in reply to 41521
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.

Re: Hexing Around

2005-08-23 15:16 • by 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!

Re: Hexing Around

2005-08-23 15:16 • by procyon112
41530 in reply to 41528
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.

Re: Hexing Around

2005-08-23 15:17 • by NoName
41531 in reply to 41525
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.

Re: Hexing Around

2005-08-23 15:18 • by whojoedaddy


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



h3 = dec2hex(d3) & h3


Re: Hexing Around

2005-08-23 15:25 • by Daveh
41534 in reply to 41532
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

Re: Hexing Around

2005-08-23 15:36 • by dubwai
41536 in reply to 41531

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.

Re: Hexing Around

2005-08-23 15:46 • by hmm
41537 in reply to 41536
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...

Re: Hexing Around

2005-08-23 15:56 • by dubwai
41539 in reply to 41537

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'.

Re: Hexing Around

2005-08-23 15:56 • by Stan Rogers
41540 in reply to 41537
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.

Re: Hexing Around

2005-08-23 15:56 • by JohnO
41541 in reply to 41537

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?

Re: Hexing Around

2005-08-23 16:00 • by hmm
41542 in reply to 41541
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...

Re: Hexing Around

2005-08-23 16:01 • by dubwai

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.

Re: Hexing Around

2005-08-23 16:03 • by christoofar
41544 in reply to 41537
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

Re: Hexing Around

2005-08-23 16:10 • by hmm
41545 in reply to 41544

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"...

Re: Hexing Around

2005-08-23 16:15 • by Volmarias
41546 in reply to 41543
I couldn't even continue reading halfway down. It was just.... terrible! Make the bad code stop :(

Re: Hexing Around

2005-08-23 16:19 • by JohnO
41547 in reply to 41542
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...



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


prints 10 in the output window.

Re: Hexing Around

2005-08-23 16:19 • by res2
41548 in reply to 41545
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) ;


 

Re: Hexing Around

2005-08-23 16:19 • by dubwai
41549 in reply to 41545
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.

Re: Hexing Around

2005-08-23 16:27 • by hmm
41551 in reply to 41547
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...



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


prints 10 in the output window.



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

Re: Hexing Around

2005-08-23 16:27 • by -L
41552 in reply to 41547
JohnO:

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




This is a joke, right?

Re: Hexing Around

2005-08-23 16:34 • by John

Ummm - are their hex numbers backwards?  Won't


  For i = Len(h1) To 1 Step -1
    d1 = hex2dec(Mid(h1,i,1))
    d2 = hex2dec(Mid(h2,i,1))


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"?


 

Re: Hexing Around

2005-08-23 16:37 • by scheky
41554 in reply to 41552
Anonymous:
JohnO:

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



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.

Re: Hexing Around

2005-08-23 16:38 • by DZ-Jay
41555 in reply to 41529
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.

Re: Hexing Around

2005-08-23 16:38 • by Perry Pederson
41556 in reply to 41545
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:


You can represent hexadecimal numbers directly by preceding numbers in the proper range with &H. For example, &H10 represents decimal 16 in hexadecimal notation.

Re: Hexing Around

2005-08-23 16:39 • by John
41557 in reply to 41553
Ooops - scratch that I forgot my MID function semantics for a minute....    

Re: Hexing Around

2005-08-23 16:41 • by Daniel T
41558 in reply to 41552
The relevant part is

Int32.Parse("A", System.Globalization.NumberStyles.HexNumber)

or

int.Parse("A", System.Globalization.NumberStyles.HexNumber)



The rest is just for output.



(
BTW, is the second way is considered a WTF)?



--Daniel T

Re: Hexing Around

2005-08-23 16:43 • by res2
41559 in reply to 41553
Anonymous:

Ummm - are their hex numbers backwards?  Won't


  For i = Len(h1) To 1 Step -1
    d1 = hex2dec(Mid(h1,i,1))
    d2 = hex2dec(Mid(h2,i,1))


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"?


 



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.


 

Re: Hexing Around

2005-08-23 16:45 • by Lazy Lurker
41560 in reply to 41552
Anonymous:
JohnO:

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



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.

Re: Hexing Around

2005-08-23 16:46 • by res2
41561 in reply to 41560
Anonymous:
Anonymous:
JohnO:

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



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 :)

Re: Hexing Around

2005-08-23 16:50 • by Daniel T
41562 in reply to 41558
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

Re: Hexing Around

2005-08-23 16:52 • by dan
41563 in reply to 41554
scheky:
Anonymous:
JohnO:

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



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



Re: Hexing Around

2005-08-23 16:55 • by dan
41565 in reply to 41563
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()

Re: Hexing Around

2005-08-23 17:05 • by foxyshadis
41567 in reply to 41565
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.

Re: Hexing Around

2005-08-23 17:15 • by JohnO
41568 in reply to 41565

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.


 

Re: Hexing Around

2005-08-23 17:18 • by Homer
41569 in reply to 41562

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!

Re: Hexing Around

2005-08-23 17:40 • by spotcatbug
41570 in reply to 41567
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).

Re: Hexing Around

2005-08-23 17:41 • by 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?

Re: Hexing Around

2005-08-23 18:06 • by John
41572 in reply to 41571
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!

Re: Hexing Around

2005-08-23 18:12 • by fmobus
41573 in reply to 41571
does this shitty code work at all?

Re: Hexing Around

2005-08-23 18:39 • by jeremydwill
41574 in reply to 41561
res2:
Anonymous:
Anonymous:
JohnO:

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



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).

Re: Hexing Around

2005-08-23 20:36 • by joe_bruin
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!

Re: Hexing Around

2005-08-23 21:26 • by grumble

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.


 

Re: Hexing Around

2005-08-23 21:56 • by heinzkunz
41583 in reply to 41560
Anonymous:
Anonymous:
JohnO:

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



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));

Re: Hexing Around

2005-08-23 22:25 • by AC
41584 in reply to 41583
Anonymous:
Anonymous:
Anonymous:
JohnO:

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



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





« PrevPage 1 | Page 2 | Page 3Next »

Add Comment