Results 1 to 14 of 14

Thread: Convert Base 10 To 255

  1. #1

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228

    Convert Base 10 To 255

    Hey guys. I need 2 functions: one that will convert a base 10 to a
    base 255. So all ASCII values would make the numbers. Get
    where im at?

    EDIT: And th eother function would convert base 255 to
    base 10.
    Luke

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    chr converts decimal to base 255 w/ ASCII representation

    asc converts ASCII-represented base 255 to decimal equivalent

  3. #3
    Lively Member FireSlash518's Avatar
    Join Date
    Nov 2001
    Posts
    67
    Remember back in algebra, when you learned how to solve proportions?

    Leader of the Maxoverkill Mods
    -Fire§lash

  4. #4

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Yes, but if i want to write 256 with this number system i would
    have to go Chr(1) & Chr(1) then 257 would be CHr(1) & Chr(2)
    etc etc...can you help me?
    Luke

  5. #5

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Originally posted by FireSlash518
    Remember back in algebra, when you learned how to solve proportions?
    I got expelled this semester. I didnt get to learn about
    proportions. Is there a thread in the maths section about this?
    Luke

  6. #6
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    That'd be base 256, not 255.

    But anyway....

    to convert from base 10 to 256 you'll have to check the value you have, then get how many spaces that'll be in the new base.

    VB Code:
    1. 'since i use this function with a number that I need a fixed length of it was written to produce a result with a fixed length, but you can change that.
    2.  
    3. Then for x = length to 1 step -1
    4. if num > (256 ^ x) then
    5. b256 = b256 & chr$(int(num/(256^x)))
    6. num = num - (int(num/(256^x))*(256^x))
    7. else
    8. b256 = b256 & chr$(0) ' not greater, so it's 0
    9. end if
    10. b256 = b256 & chr$(num)

    doing 256 to 10 is fairly easy.

    VB Code:
    1. For x = Len(Base256Chrs) - 1 To 1 Step -1
    2.         i = i + 1
    3.         rt = rt + (Asc(Mid$(Base256Chrs, x, 1)) * (256 ^ i))
    4.     Next
    5.  
    6. rt=rt + Asc(Right$(Base256Chrs, 1))

    That'll do it.

  7. #7

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Originally posted by DiGiTaIErRoR
    That'd be base 256, not 255.

    But anyway....

    to convert from base 10 to 256 you'll have to check the value you have, then get how many spaces that'll be in the new base.

    VB Code:
    1. 'since i use this function with a number that I need a fixed length of it was written to produce a result with a fixed length, but you can change that.
    2.  
    3. Then for x = length to 1 step -1
    4. if num > (256 ^ x) then
    5. b256 = b256 & chr$(int(num/(256^x)))
    6. num = num - (int(num/(256^x))*(256^x))
    7. else
    8. b256 = b256 & chr$(0) ' not greater, so it's 0
    9. end if
    10. b256 = b256 & chr$(num)

    doing 256 to 10 is fairly easy.

    VB Code:
    1. For x = Len(Base256Chrs) - 1 To 1 Step -1
    2.         i = i + 1
    3.         rt = rt + (Asc(Mid$(Base256Chrs, x, 1)) * (256 ^ i))
    4.     Next
    5.  
    6. rt=rt + Asc(Right$(Base256Chrs, 1))

    That'll do it.
    There are errors in your code and the first one does not
    work...please help.
    Luke

  8. #8
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Length = the number of characters to return.

    You also need a Next.

    num is the base 10 number.

  9. #9

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Originally posted by DiGiTaIErRoR
    Length = the number of characters to return.

    You also need a Next.

    num is the base 10 number.
    Can you post the function in it's entirety? So i can poke at it and
    see how it works a bit better?
    Luke

  10. #10
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    The whole thing:

    VB Code:
    1. Function B10To256(ByVal initval As Double, ByVal Character_Return As Integer) As String
    2. Dim rt As String
    3. Dim x As Long
    4. goal = initval
    5. DoEvents
    6. For x = Character_Return - 1 To 1 Step -1
    7.     If goal >= (256 ^ x) Then
    8.         rt = rt & Chr$(Int(goal / (256 ^ x)))
    9.         goal = goal - (Int(goal / (256 ^ x)) * (256 ^ x))
    10.     Else
    11.         rt = rt & Chr$(0)
    12.     End If
    13. Next
    14. rt = rt & Chr$(goal)
    15. B10To256 = rt
    16. End Function

  11. #11
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Since base 256 is often helpful in hex, here's a function to flipbytes, thus flip the endian.

    VB Code:
    1. Function FlipBytes(ByVal ByteTF As String, Optional ByVal ByteWidth As Long) As String
    2. Dim x As Long
    3. Dim ns As String
    4. If ByteWidth < 1 Then ByteWidth = 1
    5. If ByteWidth > 2 Then ByteWidth = 2
    6. For x = 1 To Len(ByteTF) Step ByteWidth
    7.     ns = Mid$(ByteTF, x, ByteWidth) & ns
    8. Next
    9. FlipBytes = ns
    10. End Function

    Bytewidth refers to the base representation. For an ascii base 256, it'll be 1, for hex: 2, binary: 8.

    This function now only works on hex and ascii.

  12. #12

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    it dosent work on numbers above 256. I might as well just say
    Chr(1-255)...
    Luke

  13. #13
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    richtextbox1.text = " " & b10to256(23084823,4)

  14. #14

    Thread Starter
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    How do you find out what the "Character_Return" is?
    Luke

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