Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > C++ Builder
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 19-04-2015
Avatar de aguml
aguml aguml is offline
Miembro
 
Registrado: may 2013
Posts: 885
Poder: 14
aguml Va por buen camino
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.
Responder Con Cita
  #2  
Antiguo 19-04-2015
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 38
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola aguml.
Cita:
Empezado por aguml Ver Mensaje
... 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
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #3  
Antiguo 19-04-2015
Avatar de aguml
aguml aguml is offline
Miembro
 
Registrado: may 2013
Posts: 885
Poder: 14
aguml Va por buen camino
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;
}
//--------------------------------------------------------------------------- 
Responder Con Cita
  #4  
Antiguo 22-04-2015
Avatar de aguml
aguml aguml is offline
Miembro
 
Registrado: may 2013
Posts: 885
Poder: 14
aguml Va por buen camino
Nada, llevo varios días liados y no doy con el modo correcto T_T
Responder Con Cita
  #5  
Antiguo 27-04-2015
Avatar de aguml
aguml aguml is offline
Miembro
 
Registrado: may 2013
Posts: 885
Poder: 14
aguml Va por buen camino
Lo conseguí
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 ).
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";
      }
   }
}
//--------------------------------------------------------------------------- 
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Instalar componentes de 32 bits en una PC de win7 a 64 bits uper Windows 10 29-01-2016 18:08:30
compilar programas 32 bits con lazarus 64 bits anubis Lazarus, FreePascal, Kylix, etc. 3 30-10-2013 18:08:44
Como ejecutar aplicativos dbExpress de 32 bits en 64 bits rolandoj Conexión con bases de datos 0 30-07-2010 19:39:07
Aplicaciones de 32 bits en Windows de 64 bits Gabo Debates 9 25-09-2008 20:49:32
Porque GetLocaleFormatSettings siempre retorna dd.MM.yyyy QuarkBcn API de Windows 2 11-10-2007 13:43:05


La franja horaria es GMT +2. Ahora son las 18:50:13.


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
Copyright 1996-2007 Club Delphi