Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   sscanf siempre retorna 0 en variable de 64 bits (https://www.clubdelphi.com/foros/showthread.php?t=88154)

aguml 19-04-2015 20:31:46

sscanf siempre retorna 0 en variable de 64 bits
 
Amigos tengo esta prueba que compila aunque uso sscanf y no creo que sea lo mejor y ademas sscanf siempre me retorna 0 en la variable de 64 bits.

Código PHP:

//---------------------------------------------------------------------------

#include <vcl.h>
#include <limits>
#include <stdio.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : 
TForm(Owner)
{
}
//---------------------------------------------------------------------------


bool IsOverflow (AnsiString valor)
{
   
bool overflow=false;
   
AnsiString valorLimite = std::numeric_limits<unsigned long long>::max();

   if(
valorLimite.Length() == valor.Length()){
      for (
int i=1; i <=valor.Length(); i++)
      {
         if(
valorLimite[i] > valor[i]){
            break;
         }else if (
valorLimite [i] < valor[i]){
            
overflow=true;
            break;
         }
      }
   }else if(
valorLimite.Length() > valor.Length()){
      
overflow= false;
   }else{
      
overflow=true;
   }
   return 
overflow;
}
//---------------------------------------------------------------------------

bool dividirHex(AnsiString dividendo, AnsiString divisor, AnsiString *cociente, AnsiString *resto)
{
   
unsigned __int64 auxDividendo, auxDivisor, auxCociente=0, auxResto=0;
   
bool salir=false;
   
bool retval=false;
   
int largoDividendo, largoDivisor, largoPorcion, pos;

   
largoDivisor=divisor.Length();

   if (!
IsOverflow (divisor)){
      *
cociente="";
      *
resto="";
      
sscanf(AnsiString("0x" + divisor).c_str(), "%I64u", &auxDivisor);

      do {
         
largoDividendo=dividendo.Length ();
         
largoPorcion=largoDivisor;
         if(
largoDividendo >= largoDivisor){
            
sscanf(AnsiString("0x" + dividendo.SubString(1,largoPorcion)).c_str(), "%I64u", &auxDividendo);
            if(
auxDivisor > auxDividendo){
               if(
largoDividendo > largoDivisor){
                  
largoPorcion++;
               }else{
                  break;
               }
            }
            if(
IsOverflow(dividendo.SubString(1,largoPorcion))){
               
salir=true;
               
retval=false;
            }else {
               
sscanf(AnsiString("0x" + dividendo.SubString(1,largoPorcion)).c_str(), "%I64u", &auxDividendo);
               
auxResto=auxDividendo % auxDivisor;
               
dividendo=dividendo.SubString(largoPorcion,largoDividendo);
               
auxCociente=auxDividendo / auxDivisor;
               *
cociente=*cociente + AnsiString (auxCociente);
               *
resto=AnsiString(auxResto);
               if (
auxResto > 0)
                  
dividendo= *resto + dividendo.SubString(largoPorcion,largoDividendo);
               else
                  
dividendo= dividendo.SubString(largoPorcion,largoDividendo);
               
retval=true;
            }
         }else {
            
salir=true;
            *
resto=dividendo;
            *
cociente=*cociente + "0";
            
retval=true;
         }
      }while(!
salir);
   }
   return 
retval;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   
AnsiString Dividendo="FFFFFF",Divisor="FF",Cociente,Resto;
   
dividirHex(Dividendo, Divisor, &Cociente, &Resto);
}
//--------------------------------------------------------------------------- 

A ver si me podeis ayudar.

ecfisa 19-04-2015 21:55:04

Hola aguml.
Cita:

Empezado por aguml (Mensaje 491539)
... ademas sscanf siempre me retorna 0 en la variable de 64 bits.

Por eso que comentas, entiendo que tu problema es leer el valor hexadecimal como string y luego operar con él.

Un ejemplo de como leer como string, convertirlo a hexa y operar con él:
Código PHP:

#include <iostream>
#include <sstream>

int main() {
  
std::stringstream ss;
  
unsigned long long auxDivisor;
  
std::string divisor;

  
// leer string
  
std::cin >> divisor;

  
// pasar como hexa a auxDivisor
  
std::istringstream(divisor) >> std::hex >> auxDivisor;

  
// auxDivisor / 2
  
auxDivisor >>= 1;

  
// mostrar
  
std::cout << std::hex << auxDivisor;

  return 
0;
} 

Saludos :)

aguml 19-04-2015 22:31:03

Bueno lo hice de otra manera despues de estar toda la tarde liado pero no doy con la tecla y no me sale y se convierte en un bucle infinito. Pongo lo que tengo para ver si podeis ayudarme a dejarlo bien:

Código PHP:

//---------------------------------------------------------------------------

#include <vcl.h>
#include <limits>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : 
TForm(Owner)
{
}
//---------------------------------------------------------------------------


bool IsOverflow (AnsiString valor)
{
   
bool overflow=false;
   
AnsiString valorLimite = std::numeric_limits<unsigned long long>::max();

   if(
valorLimite.Length() == valor.Length()){
      for (
int i=1; i <=valor.Length(); i++)
      {
         if(
valorLimite[i] > valor[i]){
            break;
         }else if (
valorLimite [i] < valor[i]){
            
overflow=true;
            break;
         }
      }
   }else if(
valorLimite.Length() > valor.Length()){
      
overflow= false;
   }else{
      
overflow=true;
   }
   return 
overflow;
}
//---------------------------------------------------------------------------

bool dividirHex(AnsiString dividendo, AnsiString divisor, AnsiString *cociente, AnsiString *resto)
{
   
unsigned __int64 auxDividendo, auxDivisor, auxCociente=0, auxResto=0;
   
bool salir=false;
   
bool retval=false;
   
int largoDividendo, largoDivisor, largoPorcion, pos=0;

   
largoDivisor=divisor.Length();

   if (!
IsOverflow (divisor)){
      *
cociente="";
      *
resto="";
      
auxDivisor = StrToInt64(AnsiString("0x" + divisor));

      do {
         
largoDividendo=dividendo.Length ();
         
largoPorcion=largoDivisor;

         if(
largoDividendo >= largoDivisor+pos){
            
auxDividendo = StrToInt64(AnsiString("0x" + dividendo.SubString(1,largoPorcion+pos)));
            
            if(
IsOverflow(dividendo.SubString(1,largoPorcion))){
               
salir=true;
               
retval=false;
            }else {
               
auxResto=auxDividendo % auxDivisor;
               
auxCociente=auxDividendo / auxDivisor;
               if(
auxCociente == 0){
                  
pos++;
               }else{
                  
dividendo=dividendo.SubString(largoPorcion+pos+1,largoDividendo);
                  
pos=0;
               }
               *
cociente=*cociente + AnsiString ().sprintf("%I64X", auxCociente);
               *
resto = AnsiString().sprintf("%I64X", auxResto);
               
               if (
auxResto > 0 && auxCociente > 0)
                  
dividendo= AnsiString().sprintf("%I64X", auxResto) + dividendo;
               
retval=true;
            }
         }else {
            
salir=true;
            *
cociente=*cociente + "0";
            
pos++;
            if(
dividendo != ""){
               *
resto = AnsiString().sprintf("%I64X", dividendo);
            }else{
               *
cociente=*cociente + AnsiString().sprintf("%I64X", auxCociente);
               *
resto="0";
            }
            
retval=true;
         }
      }while(!
salir);
   }
   return 
retval;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   
AnsiString Dividendo="FFAECBF",Divisor="FF",Cociente,Resto;
   
dividirHex(Dividendo, Divisor, &Cociente, &Resto);
   
EditCociente->Text = Cociente;
   
EditResto->Text = Resto;
}
//--------------------------------------------------------------------------- 


aguml 22-04-2015 08:17:39

Nada, llevo varios días liados y no doy con el modo correcto T_T

aguml 27-04-2015 13:00:03

Lo conseguí :D
Os pongo la prueba que hice para que la veais y ver si se puede mejorar de alguna manera. Tengo una funcion que divide Hexas enormes y otra que hace lo mismo con enteros. El limite me lo da el divisor ya que no se si sería posible partir la operacion en partes para que el divisor fuese mas grande (mi nivel de matematicas no da para mas :p ).
Aquí el código:
Código PHP:

//---------------------------------------------------------------------------

#include <vcl.h>
#include <limits>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

bool dividirHex(AnsiString dividendo, AnsiString divisor, AnsiString *cociente, AnsiString *resto, int *error);
bool dividirInt(AnsiString dividendo, AnsiString divisor, AnsiString *cociente, AnsiString *resto, int *error);

enum Error{SinError=0, Overflow, DivForZero};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : 
TForm(Owner)
{
}
//---------------------------------------------------------------------------

//Funcion para comprobar que un valor no sobrepase el maximo permitido para un Int64 sin signo
//El primer parametro es el valor a comprobar y el segundo parametro es para indicar si el valor
//es hexadecimal o no
bool IsOverflow(AnsiString valor, bool IsHexa)
{
   
bool overflow=false;
   
AnsiString valorLimite = std::numeric_limits<unsigned long long>::max();
   if(
IsHexa == true)
      
valorLimite = AnsiString().sprintf("%I64X", valorLimite);

   if(
valorLimite.Length() == valor.Length()){
      for(
int i=1; i <=valor.Length(); i++)
      {
         if(
valorLimite[i] > valor[i]){
            break;
         }else if (
valorLimite [i] < valor[i]){
            
overflow=true;
            break;
         }
      }
   }else if(
valorLimite.Length() > valor.Length()){
      
overflow= false;
   }else{
      
overflow=true;
   }
   return 
overflow;
}
//---------------------------------------------------------------------------

//Funcion para dividir valores hexadecimales extragrandes
//Practicamente el limite nos lo da el valor del divisor ya que el dividendo puede ser
//tan grande como el maximo menos 1 de AnsiString
bool dividirHex(AnsiString dividendo, AnsiString divisor, AnsiString *cociente, AnsiString *resto, int *error)
{
   
unsigned __int64 auxDividendo, auxDivisor, auxCociente, auxResto;
   
bool salir=false;
   
bool retval=false;
   
bool primeraPasada=true;
   
int largoDividendo, largoPorcion=1;

   
auxDivisor = StrToInt64(AnsiString("0x" + divisor));
   if(
auxDivisor == 0){
      
retval = false;
      *
error = DivForZero;
   }else{
      if(!
IsOverflow (divisor, true)){
         *
cociente="";
         *
resto="";

         do {
            
largoDividendo=dividendo.Length ();

            if(
largoDividendo != 0){
               if(
primeraPasada == true){
                  
auxDividendo = StrToInt64("0x" + dividendo.SubString(1,largoPorcion));
                  while(
auxDivisor > auxDividendo && salir != true){
                     
largoPorcion++;
                     if(
largoPorcion > largoDividendo){
                        *
cociente = "0";
                        *
resto = dividendo;
                        *
error = 0;
                        
salir = true;
                        
retval = true;
                     }
                     if(!
salir)
                        
auxDividendo = StrToInt64("0x" + dividendo.SubString(1,largoPorcion));
                  }
                  
primeraPasada=false;
               }else{
                  
largoPorcion=1;
                  
auxDividendo = StrToInt64("0x" + *resto + dividendo.SubString(1,largoPorcion));
               }

               if(!
salir){
                  
dividendo = dividendo.SubString(largoPorcion+1,largoDividendo);
                  
auxResto=auxDividendo % auxDivisor;
                  
auxCociente=auxDividendo / auxDivisor;

                  if(
auxCociente > 0){
                     *
cociente=*cociente + AnsiString ().sprintf("%I64X", auxCociente);
                     *
resto = AnsiString().sprintf("%I64X", auxResto);
                  }else{
                     *
cociente=*cociente + "0";
                     *
resto = AnsiString().sprintf("%I64X", auxResto);
                  }
               }
            }else{
               
salir=true;
               if(
primeraPasada == true){
                  *
cociente="0";
                  *
resto="0";
               }
               
retval=true;
               *
error=0;
            }
         }while(
salir != true);
      }else{
         *
error=Overflow;
         
retval=false;
      }
   }
   return 
retval;
}
//---------------------------------------------------------------------------

//Funcion para dividir valores enteros extragrandes
//Practicamente el limite nos lo da el valor del divisor ya que el dividendo puede ser
//tan grande como el maximo menos 1 de AnsiString
bool dividirInt(AnsiString dividendo, AnsiString divisor, AnsiString *cociente, AnsiString *resto, int *error)
{
   
unsigned __int64 auxDividendo, auxDivisor, auxCociente, auxResto;
   
bool salir=false;
   
bool retval=false;
   
bool primeraPasada=true;
   
int largoDividendo, largoPorcion=1;

   
auxDivisor = StrToInt64(divisor);
   if(
auxDivisor == 0){
      
retval = false;
      *
error=DivForZero;
   }else{
      if (!
IsOverflow (divisor, false)){
         *
cociente="";
         *
resto="";

         do {
            
largoDividendo=dividendo.Length ();

            if(
largoDividendo != 0){
               if(
primeraPasada == true){
                  
auxDividendo = StrToInt64(dividendo.SubString(1,largoPorcion));
                  while(
auxDivisor > auxDividendo){
                     
largoPorcion++;
                     if(
largoPorcion > largoDividendo){
                        *
cociente = "0";
                        *
resto = dividendo;
                        *
error = 0;
                        
salir = true;
                        
retval = true;
                     }
                     if(!
salir)
                        
auxDividendo = StrToInt64(dividendo.SubString(1,largoPorcion));
                  }
                  
primeraPasada=false;
               }else{
                  
largoPorcion=1;
                  
auxDividendo = StrToInt64(*resto + dividendo.SubString(1,largoPorcion));
               }
               if(!
salir){
                  
dividendo = dividendo.SubString(largoPorcion+1,largoDividendo);
                  
auxResto=auxDividendo % auxDivisor;
                  
auxCociente=auxDividendo / auxDivisor;

                  if(
auxCociente > 0){
                     *
cociente=*cociente + AnsiString(auxCociente);
                     *
resto = AnsiString(auxResto);
                  }else{
                     *
cociente=*cociente + "0";
                     *
resto = AnsiString(auxResto);
                  }
               }
            }else{
               
salir=true;
               if(
primeraPasada == true){
                  *
cociente="0";
                  *
resto="0";
               }
               
retval=true;
               *
error=0;
            }
         }while(
salir != true);
      }else{
         *
error=Overflow;
         
retval=false;
      }
   }
   return 
retval;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ButtonCalcularClick(TObject *Sender)
{
   
AnsiString Cociente,Resto;
   
int error;

   if(
RadioGroupBase->ItemIndex == 0)
      
dividirInt(EditDividendo->Text, EditDivisor->Text, &Cociente, &Resto, &error);
   else
      
dividirHex(EditDividendo->Text, EditDivisor->Text, &Cociente, &Resto, &error);
   
EditCociente->Text = Cociente;
   
EditResto->Text = Resto;

   switch(
error)
   {
      case 
0:
         
StatusBar->SimpleText = "Operacion realizada satisfactoriamente";
         break;

      case 
1:
         
StatusBar->SimpleText = "Hay overflow en el divisor";
         break;

      case 
2:
         
StatusBar->SimpleText = "No se puede dividir por 0";
         break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::EditFiltradoKeyPress(TObject *Sender, char &Key)
{
   if(
RadioGroupBase->ItemIndex == 1){
      if((
Key < '0' || Key > '9') && (Key < 'a' || Key > 'f') && (Key < 'A' || Key > 'F') && Key != '\b')
         
Key = 0;
   }else{
      if((
Key < '0' || Key > '9') && Key != '\b')
         
Key = 0;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::RadioGroupBaseClick(TObject *Sender)
{
   if(
RadioGroupBase->ItemIndex == 1)
   {  
//Lo paso a Hexa
      
if(!IsOverflow(EditDividendo->Text,false) &&
         !
IsOverflow(EditDivisor->Text,false) &&
         !
IsOverflow(EditCociente->Text,false) &&
         !
IsOverflow(EditResto->Text,false)){

            if(
EditDividendo->Text != "" &&
               
EditDivisor->Text != "" &&
               
EditCociente->Text != "" &&
               
EditResto->Text != "")
            {
               
unsigned __int64 aux;
               
aux = StrToInt64(EditDividendo->Text);
               
EditDividendo->Text = AnsiString().sprintf("%I64X", aux);
               
aux = StrToInt64(EditDivisor->Text);
               
EditDivisor->Text = AnsiString().sprintf("%I64X", aux);
               
aux = StrToInt64(EditCociente->Text);
               
EditCociente->Text = AnsiString().sprintf("%I64X", aux);
               
aux = StrToInt64(EditResto->Text);
               
EditResto->Text = AnsiString().sprintf("%I64X", aux);
               
StatusBar->SimpleText = "";
            }else{
               
StatusBar->SimpleText = "Hay algún valor no válido";
            }
      }else{
         
EditDividendo->Text = "0";
         
EditDivisor->Text = "0";
         
EditCociente->Text = "0";
         
EditResto->Text = "0";
         
StatusBar->SimpleText = "Overflow";
      }
   }else{ 
//Lo paso a Int
      
if(!IsOverflow(EditDividendo->Text,true) &&
         !
IsOverflow(EditDivisor->Text,true) &&
         !
IsOverflow(EditCociente->Text,true) &&
         !
IsOverflow(EditResto->Text,true)){

            if(
EditDividendo->Text != "" &&
               
EditDivisor->Text != "" &&
               
EditCociente->Text != "" &&
               
EditResto->Text != "")
            {
               
EditDividendo->Text = StrToInt64("0x" + EditDividendo->Text);
               
EditDivisor->Text = StrToInt64("0x" + EditDivisor->Text);
               
EditCociente->Text = StrToInt64("0x" + EditCociente->Text);
               
EditResto->Text = StrToInt64("0x" + EditResto->Text);
               
StatusBar->SimpleText = "";
            }else{
               
StatusBar->SimpleText = "Hay algún valor no válido";
            }
      }else{
         
EditDividendo->Text = "0";
         
EditDivisor->Text = "0";
         
EditCociente->Text = "0";
         
EditResto->Text = "0";
         
StatusBar->SimpleText = "Overflow";
      }
   }
}
//--------------------------------------------------------------------------- 



La franja horaria es GMT +2. Ahora son las 13:17:24.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi