Results 1 to 4 of 4

Thread: [RESOLVED] Strange Behaviour in Format function while using Hex function in VB4

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Location
    Ks
    Posts
    11

    Resolved [RESOLVED] Strange Behaviour in Format function while using Hex function in VB4

    Can someone explain why the Format function refuses to put leading zeros in front of the hexadecimal numbers A thru F in this code?


    Code:
    For idx = 1 To 15       ' actually need to loop to 1024
            TxtStr = Format(Hex(idx), "000")
        Next
    Is there a way to code this that will force the 'zeros' in front of the Hex number?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Strange Behaviour in Format function while using Hex function in VB4

    is txtstr dimmed as a string?
    Why 3 0s hex values are even number of characters so 3 is strange. Unless of course you are trying to pad 3 0s in which case you need to add a 4th as 1 or more will be replaced by the value

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Strange Behaviour in Format function while using Hex function in VB4

    You are saying: format a string to a string. Hex returns a string, not a number.
    It would be similar result as: Format("X", "000")

    One solution people generally use is to prefix the hex and take the right most n characters:
    Code:
    Right$("00" & Hex(idx), 3)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Location
    Ks
    Posts
    11

    Re: Strange Behaviour in Format function while using Hex function in VB4

    Thank You, my friend, it does exactly what I need. The original programmer was using "Format". I feel stupid to not have thought about the "Right" function (play on words :-) ). My silly work around was to just use three loops ... 1 to 15 , 16 to 255 . 256 to 1024 and prefix the correct number of zeros. See what happens when you don't use some language for 10 years!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width