evil hexadecimal numbers formatting
-
Working on an old vb6 project I found this treasure ...
Function conv8(number) As String If number = "" Then number = 0 End If conv8 = Hex(Numero) Select Case Len(conv8) Case 1 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 2 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 3 conv8 = 0 & 0 & 0 & 0 & 0 & conv8 Case 4 conv8 = 0 & 0 & 0 & 0 & conv8 Case 5 conv8 = 0 & 0 & 0 & conv8 Case 6 conv8 = 0 & 0 & conv8 Case 7 conv8 = 0 & conv8 End Select End Function
Obviously they wrote also a Conv1, Conv2, Conv4 ... and thank god there was no need for a Conv64 !!!! :)Don't write code: generate it!
-
Working on an old vb6 project I found this treasure ...
Function conv8(number) As String If number = "" Then number = 0 End If conv8 = Hex(Numero) Select Case Len(conv8) Case 1 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 2 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 3 conv8 = 0 & 0 & 0 & 0 & 0 & conv8 Case 4 conv8 = 0 & 0 & 0 & 0 & conv8 Case 5 conv8 = 0 & 0 & 0 & conv8 Case 6 conv8 = 0 & 0 & conv8 Case 7 conv8 = 0 & conv8 End Select End Function
Obviously they wrote also a Conv1, Conv2, Conv4 ... and thank god there was no need for a Conv64 !!!! :)Don't write code: generate it!
Funny. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Working on an old vb6 project I found this treasure ...
Function conv8(number) As String If number = "" Then number = 0 End If conv8 = Hex(Numero) Select Case Len(conv8) Case 1 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 2 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 3 conv8 = 0 & 0 & 0 & 0 & 0 & conv8 Case 4 conv8 = 0 & 0 & 0 & 0 & conv8 Case 5 conv8 = 0 & 0 & 0 & conv8 Case 6 conv8 = 0 & 0 & conv8 Case 7 conv8 = 0 & conv8 End Select End Function
Obviously they wrote also a Conv1, Conv2, Conv4 ... and thank god there was no need for a Conv64 !!!! :)Don't write code: generate it!
A better title would be "Left-padding Made Difficult"
-
Working on an old vb6 project I found this treasure ...
Function conv8(number) As String If number = "" Then number = 0 End If conv8 = Hex(Numero) Select Case Len(conv8) Case 1 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 2 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 3 conv8 = 0 & 0 & 0 & 0 & 0 & conv8 Case 4 conv8 = 0 & 0 & 0 & 0 & conv8 Case 5 conv8 = 0 & 0 & 0 & conv8 Case 6 conv8 = 0 & 0 & conv8 Case 7 conv8 = 0 & conv8 End Select End Function
Obviously they wrote also a Conv1, Conv2, Conv4 ... and thank god there was no need for a Conv64 !!!! :)Don't write code: generate it!
This bad on so many levels. Let's look at:
0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8
Remeber kids,
conv8
is aVariant
so it will be type checked even though it is /probably/ aString
. Every0
is anInteger
that will be cast to a string. Then EACH AND EVERY&
creates a new string, which gets thrown away with the next&
. This is so bad, I love it! Reminds me of a way back when fixed length message that we had in a system if days gone by, that was built up using athis & that & the & other
type assignment. The message was regularly being buit up and broken down. The breakdown would bethis = Left$(message,4)
message = Mid$(message. 5)
that = ...Changed it to use a function that built a fixed length string that then got updated. Boom, super fast (by VB standards) Similarly the break up was changed to only extract what was needed and used Mid$() exclusively. It still holds true today. String manipulation normally requires memory allocation. Work out what you need FIRST and reduce the re-allocs every 3 steps of the friggin way. Slightly harder to code, but when performance maters it is important.
Panic, Chaos, Destruction. My work here is done.
-
Working on an old vb6 project I found this treasure ...
Function conv8(number) As String If number = "" Then number = 0 End If conv8 = Hex(Numero) Select Case Len(conv8) Case 1 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 2 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 3 conv8 = 0 & 0 & 0 & 0 & 0 & conv8 Case 4 conv8 = 0 & 0 & 0 & 0 & conv8 Case 5 conv8 = 0 & 0 & 0 & conv8 Case 6 conv8 = 0 & 0 & conv8 Case 7 conv8 = 0 & conv8 End Select End Function
Obviously they wrote also a Conv1, Conv2, Conv4 ... and thank god there was no need for a Conv64 !!!! :)Don't write code: generate it!
Once, in the long-ago murky past, before learning the wonders of Int32.ToString("Dx"), and still being relatively new, I wrote something like this public string Pad(int val, int places) { int realplaces=0; if (val < 10) // notice that this includes all negatives, so FAIL realplaces=1; else if (val < 100) realplaces=2; else if (val < 1000) realplaces=3; ... if (realplaces >= places) return val.ToString(); return new string(' ', places - realplaces) + val.ToString(); }
-
Once, in the long-ago murky past, before learning the wonders of Int32.ToString("Dx"), and still being relatively new, I wrote something like this public string Pad(int val, int places) { int realplaces=0; if (val < 10) // notice that this includes all negatives, so FAIL realplaces=1; else if (val < 100) realplaces=2; else if (val < 1000) realplaces=3; ... if (realplaces >= places) return val.ToString(); return new string(' ', places - realplaces) + val.ToString(); }
Well, you could also learn the wonders of
Math.Log10
... ;)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Well, you could also learn the wonders of
Math.Log10
... ;)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
string s = number.ToString(); if (s.Length >= placesWanted) return s; return new string(' ', placesWanted - s.Length) + s; Because I still dislike the failure of 1.5 + 1.5 == 3
What has the above to do with
Math.Log10
? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What has the above to do with
Math.Log10
? :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Working on an old vb6 project I found this treasure ...
Function conv8(number) As String If number = "" Then number = 0 End If conv8 = Hex(Numero) Select Case Len(conv8) Case 1 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 2 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 3 conv8 = 0 & 0 & 0 & 0 & 0 & conv8 Case 4 conv8 = 0 & 0 & 0 & 0 & conv8 Case 5 conv8 = 0 & 0 & 0 & conv8 Case 6 conv8 = 0 & 0 & conv8 Case 7 conv8 = 0 & conv8 End Select End Function
Obviously they wrote also a Conv1, Conv2, Conv4 ... and thank god there was no need for a Conv64 !!!! :)Don't write code: generate it!
Stefano Basili wrote:
thank god there was no need for a Conv64 !!!!
No kidding. It would be ugly, but doable with just loads of copying/pasting :rolleyes:
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
This bad on so many levels. Let's look at:
0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8
Remeber kids,
conv8
is aVariant
so it will be type checked even though it is /probably/ aString
. Every0
is anInteger
that will be cast to a string. Then EACH AND EVERY&
creates a new string, which gets thrown away with the next&
. This is so bad, I love it! Reminds me of a way back when fixed length message that we had in a system if days gone by, that was built up using athis & that & the & other
type assignment. The message was regularly being buit up and broken down. The breakdown would bethis = Left$(message,4)
message = Mid$(message. 5)
that = ...Changed it to use a function that built a fixed length string that then got updated. Boom, super fast (by VB standards) Similarly the break up was changed to only extract what was needed and used Mid$() exclusively. It still holds true today. String manipulation normally requires memory allocation. Work out what you need FIRST and reduce the re-allocs every 3 steps of the friggin way. Slightly harder to code, but when performance maters it is important.
Panic, Chaos, Destruction. My work here is done.
williamnw wrote:
Every 0 is an Integer that will be cast to a string. Then EACH AND EVERY & creates a new string, which gets thrown away with the next &.
For all its faults, the VB6 compiler is a little better than that - it is capable of concatenating string literals at compile time. I remember maintaining a report-writing program that abused VB's string handling similarly. It created an RTF report, but generally appended characters pretty much 1 to 5 at a time, and the eventual reports ran up to about 14MB as RTF, so you can imagine how many string copy's resulted there. I ported the core of the engine to C++, resulting in a speedup well over one thousand fold!
-
Working on an old vb6 project I found this treasure ...
Function conv8(number) As String If number = "" Then number = 0 End If conv8 = Hex(Numero) Select Case Len(conv8) Case 1 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 2 conv8 = 0 & 0 & 0 & 0 & 0 & 0 & conv8 Case 3 conv8 = 0 & 0 & 0 & 0 & 0 & conv8 Case 4 conv8 = 0 & 0 & 0 & 0 & conv8 Case 5 conv8 = 0 & 0 & 0 & conv8 Case 6 conv8 = 0 & 0 & conv8 Case 7 conv8 = 0 & conv8 End Select End Function
Obviously they wrote also a Conv1, Conv2, Conv4 ... and thank god there was no need for a Conv64 !!!! :)Don't write code: generate it!
Hey! Did you post this code with the author's consent? Gosh, he must've had a hard time on this. Haha
-
Hey! Did you post this code with the author's consent? Gosh, he must've had a hard time on this. Haha
Sure ... and the original code was even worse than that! :)
Don't write code: generate it!