Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. evil hexadecimal numbers formatting

evil hexadecimal numbers formatting

Scheduled Pinned Locked Moved The Weird and The Wonderful
13 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Stefano Basili

    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!

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #3

    A better title would be "Left-padding Made Difficult"

    1 Reply Last reply
    0
    • S Stefano Basili

      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!

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #4

      This bad on so many levels. Let's look at:

      0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8

      Remeber kids, conv8 is a Variant so it will be type checked even though it is /probably/ a String. 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 &. 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 a this & that & the & other type assignment. The message was regularly being buit up and broken down. The breakdown would be

      this = 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.

      R 1 Reply Last reply
      0
      • S Stefano Basili

        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!

        J Offline
        J Offline
        jamie550
        wrote on last edited by
        #5

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

        C 1 Reply Last reply
        0
        • J jamie550

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

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #6

          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]

          J 1 Reply Last reply
          0
          • C CPallini

            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]

            J Offline
            J Offline
            jamie550
            wrote on last edited by
            #7

            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

            C 1 Reply Last reply
            0
            • J jamie550

              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

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #8

              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]

              J 1 Reply Last reply
              0
              • C CPallini

                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]

                J Offline
                J Offline
                jamie550
                wrote on last edited by
                #9

                I prefer to avoid doubles when possible, even if it's a stupid idea. And why wastefully calculate the log when you already have the actual length of the number right in your hands?

                1 Reply Last reply
                0
                • S Stefano Basili

                  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!

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #10

                  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

                  1 Reply Last reply
                  0
                  • N Nagy Vilmos

                    This bad on so many levels. Let's look at:

                    0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8

                    Remeber kids, conv8 is a Variant so it will be type checked even though it is /probably/ a String. 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 &. 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 a this & that & the & other type assignment. The message was regularly being buit up and broken down. The breakdown would be

                    this = 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.

                    R Offline
                    R Offline
                    Rob Grainger
                    wrote on last edited by
                    #11

                    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!

                    1 Reply Last reply
                    0
                    • S Stefano Basili

                      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!

                      L Offline
                      L Offline
                      Louis Cipher
                      wrote on last edited by
                      #12

                      Hey! Did you post this code with the author's consent? Gosh, he must've had a hard time on this. Haha

                      S 1 Reply Last reply
                      0
                      • L Louis Cipher

                        Hey! Did you post this code with the author's consent? Gosh, he must've had a hard time on this. Haha

                        S Offline
                        S Offline
                        Stefano Basili
                        wrote on last edited by
                        #13

                        Sure ... and the original code was even worse than that! :)

                        Don't write code: generate it!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups