Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [VB6]Base 64

    Ciao a tutti
    Sto creando un programmino che mi trsfarmi i file in Base 64. Su wikipidia ho trovato http://it.wikipedia.org/wiki/Base64.

    Allora ho fatto in questo modo:

    'tb64 è la sub principale che passandogli una directory,
    ' dovrebbe crearmi un altro file in C:\file.txt
    sub tb64(dirf)
    set file = fso.getfile(dirf)
    set leggi = file.openastextstream(1)
    set scrivi = fso.createtextfile("C:\file.txt")

    for a = 1 to fix(file.size / 3)
    cod = ""
    for b = 1 to 3
    tmp = dtb(asc(leggi.read(1)))
    cod = cod & String(8 - len(tmp), "0") & tmp
    next
    for b = 0 to 24 step + 6
    tr btd(mid(cod, b+1, 6))
    next
    next

    select case (file.size mod 3)
    case 1
    tmp = dtb(asc(leggi.read(1)))
    cod = cod & String(8 - len(tmp), "0") & tmp
    tr btd(mid(cod, 1, 6))
    tr btd("0000" & mid(cod, 7))
    case 2
    tmp = dtb(asc(leggi.read(1)))
    cod = cod & String(8 - len(tmp), "0") & tmp
    tr btd(mid(cod, 1, 6))
    tr btd(mid(cod, 7, 12))
    tr btd("00" & mid(cod, 13))
    end select
    msgbox "fine"
    end sub

    function eleva(base, esponente)
    if esponente = 0 then
    eleva = 1
    elseif esponente > 0 then
    eleva = 1
    for a = 1 to esponente
    eleva = eleva * base
    next
    else
    eleva = 1
    for a = -1 to esponente step -1
    eleva = eleva * base
    next
    eleva = 1/eleva
    end if
    end function

    sub tr(codice)
    select case codice
    case 0
    scrivi.write "A"
    case 1
    scrivi.write "B"
    case 2
    scrivi.write "C"
    case 3
    scrivi.write "D"
    case 4
    scrivi.write "E"
    case 5
    scrivi.write "F"
    case 6
    scrivi.write "G"
    case 7
    scrivi.write "H"
    case 8
    scrivi.write "I"
    case 9
    scrivi.write "J"
    case 10
    scrivi.write "K"
    case 11
    scrivi.write "L"
    case 12
    scrivi.write "M"
    case 13
    scrivi.write "N"
    case 14
    scrivi.write "O"
    case 15
    scrivi.write "P"
    case 16
    scrivi.write "Q"
    case 17
    scrivi.write "R"
    case 18
    scrivi.write "S"
    case 19
    scrivi.write "T"
    case 20
    scrivi.write "U"
    case 21
    scrivi.write "V"
    case 22
    scrivi.write "W"
    case 23
    scrivi.write "X"
    case 24
    scrivi.write "Y"
    case 25
    scrivi.write "Z"
    case 26
    scrivi.write "a"
    case 27
    scrivi.write "b"
    case 28
    scrivi.write "c"
    case 29
    scrivi.write "d"
    case 30
    scrivi.write "e"
    case 31
    scrivi.write "f"
    case 32
    scrivi.write "g"
    case 33
    scrivi.write "h"
    case 34
    scrivi.write "i"
    case 35
    scrivi.write "j"
    case 36
    scrivi.write "k"
    case 37
    scrivi.write "l"
    case 38
    scrivi.write "m"
    case 39
    scrivi.write "n"
    case 40
    scrivi.write "o"
    case 41
    scrivi.write "p"
    case 42
    scrivi.write "q"
    case 43
    scrivi.write "r"
    case 44
    scrivi.write "s"
    case 45
    scrivi.write "t"
    case 46
    scrivi.write "u"
    case 47
    scrivi.write "v"
    case 48
    scrivi.write "w"
    case 49
    scrivi.write "x"
    case 50
    scrivi.write "y"
    case 51
    scrivi.write "z"
    case 52
    scrivi.write "0"
    case 53
    scrivi.write "1"
    case 54
    scrivi.write "2"
    case 55
    scrivi.write "3"
    case 56
    scrivi.write "4"
    case 57
    scrivi.write "5"
    case 58
    scrivi.write "6"
    case 59
    scrivi.write "7"
    case 60
    scrivi.write "8"
    case 61
    scrivi.write "9"
    case 62
    scrivi.write "+"
    case 63
    scrivi.write "/"
    end select
    end sub

    function btd(binario)
    btd = 0
    lun = len(binario)
    for a = 1 to lun
    if cint(mid(binario, a, 1)) = 1 then btd = btd + eleva(2, lun - a)
    next
    end function

    function dtb(decimale)
    dtb = ""
    ris = decimale
    do
    dtb = dtb & (ris mod 2)
    ris = fix(ris / 2)
    loop until ris = 0
    dtb = clng(StrReverse(dtb))
    end function

    ho provato con un file .gif di 64 byte(circa) è mi è venuto:
    R0lGAODlhAAQABAAID/AAMDAAwAAAAACH5ABAEAAAAAAALAAAAAAABAAAEAAAAICARAEA AR

    mentre se provo a inviarmi il file con un e-mail mi viene:
    R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==

    perchè tutte queste A di troppo?

    grazie
    ( \_/ )
    ( *.* )
    ( > < ) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,472

    Moderazione

    Ho spostato la discussione all'interno del forum dedicato a Visual Basic (VBx, VBA, ...) e ai linguaggi per .NET Framework (VB.NET, C#, ...).

    In futuro, poni in questo forum le domande relative a questi linguaggi.

    Ciao!
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    infatti io volevo metterlo qua però non so perche me la messo in programmazione...
    ( \_/ )
    ( *.* )
    ( > < ) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  4. #4
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,472
    Originariamente inviato da nanu_90
    infatti io volevo metterlo qua però non so perche me la messo in programmazione...
    Probabilmente, non eri qui...
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  5. #5
    UP!!
    ( \_/ )
    ( *.* )
    ( > < ) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  6. #6
    Ho risolto in parte il problema:

    sub tb64(dirf)
    set file = fso.getfile(dirf)
    set leggi = file.openastextstream(1)
    set scrivi = fso.createtextfile("C:\file.txt")

    for a = 1 to fix(file.size / 3)
    cod = ""
    for b = 1 to 3
    tmp = dtb(asc(leggi.read(1)))
    cod = cod & String(8 - len(tmp), "0") & tmp
    next
    for b = 0 to 18 step + 6 ' ERA QUI L'ERRORE... 18 NON 24 PERCHE PARTO DA 0
    tr btd(mid(cod, b+1, 6))


    Ora il messaggio che mi viene fuori è
    R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAR

    mentre quello che mi dovrebbe venire è
    R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==


    Evidentemente c'è qualcosa che non va in quel select case alla fine della sub tb64
    ( \_/ )
    ( *.* )
    ( > < ) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  7. #7
    Ho risolto il problema nel select

    case 1
    tmp = dtb(asc(leggi.read(1)))
    cod = cod & String(8 - len(tmp), "0") & tmp
    tr btd(mid(cod, 1, 6))
    tr btd(mid(cod, 7) & "0000") ' non tr btd("0000" & mid(cod, 7))
    case 2
    tmp = dtb(asc(leggi.read(1)))
    cod = cod & String(8 - len(tmp), "0") & tmp
    tr btd(mid(cod, 1, 6))
    tr btd(mid(cod, 7, 12))
    tr btd(mid(cod, 13) & "00") ' non tr btd("00" & mid(cod, 7))

    Finalmente i risultati coincidono. Buon Natale!
    ( \_/ )
    ( *.* )
    ( > < ) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.