views:

859

answers:

3

Hello

I wish to convert a 32bit Long to a String*6 which will only use the characters [A-Z].

Using VB6 (don't ask!).

I've calculated that a single letter uses 5 bits, so I can get 6 letters out of a 32 bit long.

Can anyone give me a pointer on how to do this, as I have no clue.

+1  A: 

Your math is seriously flawed. 5 bits x 6 letters = 30 bits, 2 shy of what you need. Plus, 5 bits requires 2 ^ 5 = 32 codes, you've only got 26 (A-Z). Given that you want A-Z, you can only encode 4 bits. Which requires 32 / 4 = 8 letters. Exactly as many as you'd get if you encode in hex.

Use the Hex() function and String*8.

Hans Passant
A: 

A single letter uses 5 bits

  • correct

I can get 6 letters out of a 32 bit long.

  • You can encode 6 letters into a 32-bit long.

But that doesn't mean you can encode every 32-bit long into 6 letters.

Because 32^6 (1,073,741,824) < 2^32 (4,294,967,296)

That's even assuming you have 32 possible values (5-bits) per character.

It's even worse with 26 letters (which is less than 32-bits): 26^6 = 308,915,776

Cade Roux
A: 

This two function should get you close to what you want. The first one uses numbers and letters. The second one letters only. MaxChar is the number of letters you want to use using the scale 01234567890ABCDEFGHIGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. Alpha only doesn't use 0 to 9.

Public Function PackNumber(ByVal Value As Long, ByVal MaxChar As Integer, ByRef IsNeg As Boolean) As String
    Dim CurValue As Currency
    Dim CharValue As Integer
    Dim sPacked As String

    If Sgn(n) = -1 Then
        IsNeg = True
        CurValue = -Value
    Else
        IsNeg = False
        CurValue = Value
    End If

    sPacked = ""
    Do Until Abs(CurValue) <= MaxChar
        CharValue = MaxChar * ((CurValue / MaxChar) - Int(CurValue / MaxChar))
        CurValue = Int(CurValue / MaxChar)
        If CharValue < 10 Then
            sPacked = CharValue & sPacked
        ElseIf CharValue <= 36 Then
            sPacked = Chr(55 + CharValue) & sPacked
        Else
            sPacked = Chr(60 + CharValue) & sPacked
        End If
    Loop

    CharValue = CurValue
    If CharValue < 10 Then
        sPacked = CStr(CharValue) & sPacked
    ElseIf CharValue <= 36 Then
        sPacked = Chr(55 + CharValue) & sPacked
    Else
        sPacked = Chr(60 + CharValue) & sPacked
    End If

    PackNumber = sPacked
End Function

The Alpha Only Function

Public Function PackNumberAlphaOnly(ByVal Value As Long, ByVal MaxChar As Integer, ByRef IsNeg As Boolean) As String
    Dim CurValue As Currency
    Dim CharValue As Integer
    Dim sPacked As String

    If Sgn(Value) = -1 Then
        IsNeg = True
        CurValue = -Value
    Else
        IsNeg = False
        CurValue = Value
    End If

    sPacked = ""
    Do Until Abs(CurValue) <= MaxChar
        CharValue = MaxChar * ((CurValue / MaxChar) - Int(CurValue / MaxChar))
        CurValue = Int(CurValue / MaxChar)
        If CharValue <= 26 Then
            sPacked = Chr(65 + CharValue) & sPacked
        Else
            sPacked = Chr(70 + CharValue) & sPacked
        End If
    Loop

    CharValue = CurValue
    If CharValue <= 26 Then
        sPacked = Chr(65 + CharValue) & sPacked
    Else
        sPacked = Chr(70 + CharValue) & sPacked
    End If

    PackNumberAlphaOnly = sPacked
End Function
RS Conley