Page 1 of 1

funcion C a xBase

PostPosted: Mon Apr 22, 2024 12:43 pm
by JoseAlvarez
Hola a todos, amigos del foro.

Tengo la necesidad de usar una funcion que esta hecha en C .

Se puede usar directamente? cómo ? o hay que pasarla a xBase?

En caso de que haya que pasarla, puede alguien darme una mano? no tengo idea de C.

Este es el codigo:

Code: Select all  Expand view
public bool validarRif(string sRif)
        {
            bool bResultado = false;
            int iFactor = 0;

            sRif = sRif.Replace("-", "");
            if (sRif.Length < 10)
                sRif = sRif.ToUpper().Substring(0, 1) + sRif.Substring(1, sRif.Length - 1).PadLeft(9, '0');

             string sPrimerCaracter = sRif.Substring(0, 1).ToUpper();

            switch (sPrimerCaracter)
            {
                case "V": iFactor = 1; break;
                case "E": iFactor = 2; break;
                case "J": iFactor = 3; break;
                case "P": iFactor = 4; break;
                case "G": iFactor = 5; break;
            }
            if (iFactor > 0)
            {
                int suma = ((int.Parse(sRif.Substring(8, 1))) * 2)
                         + ((int.Parse(sRif.Substring(7, 1))) * 3)
                         + ((int.Parse(sRif.Substring(6, 1))) * 4)
                         + ((int.Parse(sRif.Substring(5, 1))) * 5)
                         + ((int.Parse(sRif.Substring(4, 1))) * 6)
                         + ((int.Parse(sRif.Substring(3, 1))) * 7)
                         + ((int.Parse(sRif.Substring(2, 1))) * 2)
                         + ((int.Parse(sRif.Substring(1, 1))) * 3)
                         + (iFactor * 4);

                float dividendo = suma / 11;
                int DividendoEntero = (int)dividendo;
                int resto = 11 - (suma - DividendoEntero * 11);
                if (resto >= 10 || resto < 1)
                    resto = 0;
                if (sRif.Substring(9, 1).Equals(resto.ToString()))
                {
                    bResultado = true;
                }

            }
         
            return bResultado;

        }


Se ve muy sencilla y hasta se entiende, pero ya sabemos que en programación una coma o punto hace la diferencia entre bien y mal.

Como siempre, agradezco de antemano la ayuda que puedan prestarme.

Gracias !!

Re: funcion C a xBase

PostPosted: Mon Apr 22, 2024 3:55 pm
by Enrico Maria Giordano
This is not C. C does not have a "string" type.

Re: funcion C a xBase

PostPosted: Mon Apr 22, 2024 3:58 pm
by Enrico Maria Giordano
Ok, it is C# under .NET.

Re: funcion C a xBase

PostPosted: Mon Apr 22, 2024 4:49 pm
by JoseAlvarez
Hello Enrico.

I really don't know anything about C or C# or .NET.

I got the function by searching on google.

If you can help me, I welcome your help.

Re: funcion C a xBase

PostPosted: Mon Apr 22, 2024 4:52 pm
by Enrico Maria Giordano
Try asking to the AI. :-)

Re: funcion C a xBase

PostPosted: Mon Apr 22, 2024 4:53 pm
by JoseAlvarez
Good idea, i will

Thanks!

Re: funcion C a xBase

PostPosted: Mon Apr 22, 2024 5:37 pm
by Antonio Linares
José,

Mira a ver si esta te funciona bien: https://huggingface.co/chat/conversation/66269e0c7016e222bd762b89
Code: Select all  Expand view
FUNCTION validarRif( sRif )
   LOCAL bResultado :=.F.
   LOCAL iFactor := 0
   LOCAL sPrimerCaracter
   LOCAL suma
   LOCAL dividendo
   LOCAL DividendoEntero
   LOCAL resto

   sRif := StrTran( sRif, "-", "" )
   IF Len( sRif ) < 10
      sRif := Upper( Left( sRif, 1 ) ) + PadL( SubStr( sRif, 2 ), 9, '0' )
   ENDIF

   sPrimerCaracter := Upper( Left( sRif, 1 ) )

   DO CASE
      CASE sPrimerCaracter == "V"
         iFactor := 1
      CASE sPrimerCaracter == "E"
         iFactor := 2
      CASE sPrimerCaracter == "J"
         iFactor := 3
      CASE sPrimerCaracter == "P"
         iFactor := 4
      CASE sPrimerCaracter == "G"
         iFactor := 5
   ENDCASE

   IF iFactor > 0
      suma := ( Val( SubStr( sRif, 9, 1 ) ) * 2 ) + ;
         ( Val( SubStr( sRif, 8, 1 ) ) * 3 ) + ;
         ( Val( SubStr( sRif, 7, 1 ) ) * 4 ) + ;
         ( Val( SubStr( sRif, 6, 1 ) ) * 5 ) + ;
         ( Val( SubStr( sRif, 5, 1 ) ) * 6 ) + ;
         ( Val( SubStr( sRif, 4, 1 ) ) * 7 ) + ;
         ( Val( SubStr( sRif, 3, 1 ) ) * 2 ) + ;
         ( Val( SubStr( sRif, 2, 1 ) ) * 3 ) + ;
         ( iFactor * 4 )

      dividendo := suma / 11
      DividendoEntero := Int( dividendo )
      resto := 11 - ( suma - DividendoEntero * 11 )
      IF resto >= 10.OR. resto < 1
         resto := 0
      ENDIF
      IF SubStr( sRif, 9, 1 ) == Str( resto )
         bResultado :=.T.
      ENDIF
   ENDIF

   RETURN bResultado

Based on the function logic, I'll calculate some valid RIFs and check if they are correct.

Here are a few examples:

1. `V123456780`
* `suma` = 142
* `dividendo` = 12.909090...
* `DividendoEntero` = 12
* `resto` = 0
* `SubStr( sRif, 9, 1 )` = "0" and `Str( resto )` = "0", so this RIF is valid.

2. `E123456781`
* `suma` = 143
* `dividendo` = 13.0
* `DividendoEntero` = 13
* `resto` = 0
* `SubStr( sRif, 9, 1 )` = "1" and `Str( resto )` = "0", so this RIF is not valid.

3. `J123456782`
* `suma` = 144
* `dividendo` = 13.090909...
* `DividendoEntero` = 13
* `resto` = 1
* `SubStr( sRif, 9, 1 )` = "2" and `Str( resto )` = "1", so this RIF is valid.

4. `P123456783`
* `suma` = 145
* `dividendo` = 13.181818...
* `DividendoEntero` = 13
* `resto` = 2
* `SubStr( sRif, 9, 1 )` = "3" and `Str( resto )` = "2", so this RIF is valid.

5. `G123456784`
* `suma` = 146
* `dividendo` = 13.272727...
* `DividendoEntero` = 13
* `resto` = 3
* `SubStr( sRif, 9, 1 )` = "4" and `Str( resto )` = "3", so this RIF is valid.

Please note that these calculations are based on the function logic provided, and the validity of the RIFs depends on the specific rules and regulations of the Venezuelan RIF system.