Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    123

    C++ Chi e' in grado di tradurlo?

    Chi e' in grado di tradurre questa funzione scritta in c++ in vb6?

    int fieee2msbin(float *src, float *dst) {
    union {
    float a;
    u_long b;
    } c;
    u_short man;
    u_short exp;

    c.a = *src;
    if (c.b) { /* not zero */
    man = c.b >> 16;
    exp = ((man << 1) & 0xff00) + 0x0200;
    if (exp & 0x8000 != (man << 1) & 0x8000)
    return 1; /* exponent overflow */
    man = man & 0x7f | (man >> 8) & 0x80; /*
    move sign */
    man |= exp;
    c.b = c.b & 0xffff | (long)man << 16;
    }
    *dst = c.a;
    return 0;
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2001
    Messaggi
    167
    Guarda, il problema principale è dato dalla presenza di << e >> per lo shift dei bit, perchè in C e C++ vengono utilizzati tipi di variabili unsigned che in VB non esistono, e quindi in VB ci sono problemi, quindi scriviti in un modulo queste funzioni per lo shift e testala (l'ho trovata in rete)

    codice:
    Option Explicit 
    Private m_lPower2(0 To 31) As Long 
    Public Function RShift(ByVal lThis As Long, ByVal lBits As Long) As Long
    If (lBits <= 0) Then
    RShift = lThis
    ElseIf (lBits > 63) Then
    ' .. error ..
    ElseIf (lBits > 31) Then
    RShift = 0
    Else
    If (lThis And m_lPower2(31 - lBits)) = m_lPower2(31 - lBits) Then
    RShift = (lThis And (m_lPower2(31 - lBits) - 1)) * m_lPower2(lBits) Or m_lPower2(31)
    Else
    RShift = (lThis And (m_lPower2(31 - lBits) - 1)) * m_lPower2(lBits)
    End If
    End If
    End Function 
    Public Function LShift(ByVal lThis As Long, ByVal lBits As Long) As Long
    If (lBits <= 0) Then
    LShift = lThis
    ElseIf (lBits > 63) Then
    ' ... error ..
    ElseIf (lBits > 31) Then
    LShift = 0
    Else
    If (lThis And m_lPower2(31)) = m_lPower2(31) Then
    LShift = (lThis And &H7FFFFFFF) \ m_lPower2(lBits) Or m_lPower2(31 - lBits)
    Else
    LShift = lThis \ m_lPower2(lBits)
    End If
    End If
    End Function 
    Public Sub Init()
    m_lPower2(0) = &H1&
    m_lPower2(1) = &H2&
    m_lPower2(2) = &H4&
    m_lPower2(3) = &H8&
    m_lPower2(4) = &H10&
    m_lPower2(5) = &H20&
    m_lPower2(6) = &H40&
    m_lPower2(7) = &H80&
    m_lPower2(8) = &H100&
    m_lPower2(9) = &H200&
    m_lPower2(10) = &H400&
    m_lPower2(11) = &H800&
    m_lPower2(12) = &H1000&
    m_lPower2(13) = &H2000&
    m_lPower2(14) = &H4000&
    m_lPower2(15) = &H8000&
    m_lPower2(16) = &H10000
    m_lPower2(17) = &H20000
    m_lPower2(18) = &H40000
    m_lPower2(19) = &H80000
    m_lPower2(20) = &H100000
    m_lPower2(21) = &H200000
    m_lPower2(22) = &H400000
    m_lPower2(23) = &H800000
    m_lPower2(24) = &H1000000
    m_lPower2(25) = &H2000000
    m_lPower2(26) = &H4000000
    m_lPower2(27) = &H8000000
    m_lPower2(28) = &H10000000
    m_lPower2(29) = &H20000000
    m_lPower2(30) = &H40000000
    m_lPower2(31) = &H80000000
    End Sub
    Per testarle chiama init, poi le funzioni chiamale così (in questo esempio shifta di 8 a destra:
    codice:
    Dim lValue As Long 
    lValue = &HFFFFFFFF& ' 
    Debug.Print RShift(lValue,8)
    Per il resto del codice, qua utilizzo dei long (poi controlla tu i tipi delle variabili
    codice:
     
    
    Option Explicit
    ' Questa equivale alla union
    Type c
        a As Long
        b As Long
    End Type
    ' Questa è la traduzione della funzione che hai scritto (testala perchè con i valori in esadecimale può darsi che ci siano errori poi il Vb non lo conosco bene)

    codice:
    Public Function MyFunction(src As Long, dst As Long) As Long
    
    
    Dim man As Long
    Dim exp As Long
    Dim xx As c
    
    xx.a = src
    
    If xx.b <> 0 Then
    
        man = RShift(xx.b, 16)
        exp = (LShift(man, 1) And &HFF00&) + &H200
        If (exp And &H8000&) <> (LShift(man, 1) And &H8000&) Then
            MyFunction = 1
            man = man And &H7F Or RShift(man, 8) And &H80&
            man = man Or exp
            xx.b = xx.b And &HFFFF& Or LShift(CLng(man), 16)
        End If
        dst = xx.a
        MyFunction = 0
    End If
    End Function

    Ciao.

  3. #3
    Level, scusa se ti interrompo ma.. invece di fare tutta quella lunga funzione per gli shift, perché non usi "numero * (2 ^ numero_di_shift)" (l-shift) e "numero / (2 ^ numero_di_shift)" (r-shift)?

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2001
    Messaggi
    167
    Originariamente inviato da r0x
    Level, scusa se ti interrompo ma.. invece di fare tutta quella lunga funzione per gli shift, perché non usi "numero * (2 ^ numero_di_shift)" (l-shift) e "numero / (2 ^ numero_di_shift)" (r-shift)?
    Quella non l'ho fatta io, l'ho presa pari pari in rete. E' semplicemente una prova in cui carica l'array per verificare ogni valore dello shift per le potenze di 2.
    Purtroppo il tempo è quello che è, mi son connesso solo ieri sera al forum e quindi non l'ho testato.

    ciao.

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    123

    Garzie

    Ti ringrazio Level sei stato utilissimo!!
    Spero di poter un giorno ricambiare!
    Sono riuscito a trovare in rete una dll scritta in VB che fa questo e molte altre cose che mi servivano!
    :quote: Cr4edo che utilizzero' quella!
    Se a qualcuno puo' servire non deve fare altro che chiedermela via e-mail
    Grazie

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.