Posts

Showing posts from February, 2010

C++: From byte to signed int

Spanish version / Versión en Español If we cast a byte, that is, an unsigned char, to and int, we'll get a value between 0 and 255, since int, even thpugh it's signed, contains that range of values. If our intention is to see that byte as a value between -128 and 127, we must cast to signed char before casting to int. So watch it. unsigned char b = 248; int i = static_cast<int>(b); //248 int j = static_cast<int>( static_cast<char>(b) ); //-8 I prefer using static_cast to the traditional, equivalent casting style because it makes casts easier to find when you suspect there is a problem caused by them. (it's easier to find "static_cast<int>" than "(int)" )

C++: De byte a signed int

Versión en inglés / English version Si casteamos un byte, o sea un unsigned char, a int, obtendremos un valor entre 0 y 255, ya que int, pese a ser signed, contiene ese rango de valores. Si nuestra intención es ver ese byte como un entero entre -128 y 127, lo que debemos hacer es primero castear a signed char. Y recién después de hacer eso, castear a int. unsigned char b = 248; int i = static_cast<int>(b); //248 int j = static_cast<int>( static_cast<char>(b) ); //-8 Prefiero usar static_cast antes que la forma tradicional (y equivalente) de castear, porque facilita encontrar los casteos cuando uno sospecha que generan problemas (es más fácil encontrar "static_cast<int>" que "(int)" ).

C++: How to see a byte's bits

Spanish version / Versión en Español If you ever go down to the dungeons to slay a draconic bug, this little function, adapted from the one written by Bruce Eckel (see Reference), may be a helpful torch. string printBinary(const unsigned char val) { string binaryString=""; for(int i = 7; i >= 0; i--){ if( val & (1 << i) ){ binaryString += "1"; }else{ binaryString += "0"; } } return binaryString; } Reference: Thinking in C++, Vol One, Chapter 3: The C in C++, section: "Shift operators"

C++: Cómo ver los bits de un byte

Versión en inglés / English version Si algún día tenemos necesidad de bajar a las mazmorras para matar un bug dracónico, esta funcioncita, adaptada de la que escribió Bruce Eckel para "Thinking in C++" (ver referencia), puede servir de antorcha: string printBinary(const unsigned char val) { string binaryString=""; for(int i = 7; i >= 0; i--){ if( val & (1 << i) ){ binaryString += "1"; }else{ binaryString += "0"; } } return binaryString; } Referencia: Thinking in C++, Vol One, Chapter 3: The C in C++, section: "Shift operators"

Tracking down exceptions in Visual Studio 2008

Spanish version / Versión en Español If you ever looked at Visual Studio's Output window and saw a "first chance exception" message and a weird error which is not even documented in the MSDN, and you were not patient enough to debug step by step until the line popped up in the Output window again, I have a solution for you. It also applies if you want the Visual Studio debugger to break every time an exception is thrown and/or unhandled by your code. The secret is Visual Studio's Debug->Exceptions dialog. Here we can check which exceptions to track down, including .NET, native C++, ATL and stuff. In the particular case of C++ exceptions, for this mechanism to work, certain conditions must be met: When installing Visual Studio, the C++ runtime was installed In the C++ project's general properties, "Use of MFC" is set to "Use MFC in a static library" or "Use Standard Windows Library". In the project's properti

Rastreando excepciones en Visual Studio 2008

Versión en inglés / English version Si alguna vez miraron el Output del Visual Studio y vieron "first chance exception" y un mensaje raro que no aparecía documentado ni en la MSDN, y no les daba la paciencia para hacer paso a paso con el debugger hasta que apareciera la línea en el Output, ésta es la solución. También si les salta alguna excepción de .NET y el Visual no les pone el breakpoint cuando eso ocurre. El secreto está en el diálogo Debug->Exceptions. Aquí podemos tildar las excepciones que queremos detectar, para que se ponga un breakpoint cuando salten. Pueden ser generadas por .NET, el runtime de C++, ATL y otras yerbas. En el caso particular de las excepciones de C++, para que este mecanismo funcione, debe cumplirse que: Al instalar el Visual, se instaló el runtime de C++ En las propiedades generales del proyecto, "Use of MFC" está seteado en "Use MFC in a static library" o "Use Standard Windows Library". En las