Fix and document wxDecToHex()
1. Integer instead of floating point arithmetic should be used in calculations. 2. Because this function always returns 2 hexadecimal characters, its parameter type should be changed from 'int' to 'unsigned char' to avoid misunderstandings and prevent misuses. Closes #12612.
This commit is contained in:
@@ -309,9 +309,9 @@ inline int wxHexToDec(const char* buf)
|
|||||||
|
|
||||||
|
|
||||||
// Convert decimal integer to 2-character hex string
|
// Convert decimal integer to 2-character hex string
|
||||||
WXDLLIMPEXP_BASE void wxDecToHex(int dec, wxChar *buf);
|
WXDLLIMPEXP_BASE void wxDecToHex(unsigned char dec, wxChar *buf);
|
||||||
WXDLLIMPEXP_BASE void wxDecToHex(int dec, char* ch1, char* ch2);
|
WXDLLIMPEXP_BASE void wxDecToHex(unsigned char dec, char* ch1, char* ch2);
|
||||||
WXDLLIMPEXP_BASE wxString wxDecToHex(int dec);
|
WXDLLIMPEXP_BASE wxString wxDecToHex(unsigned char dec);
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// Process management
|
// Process management
|
||||||
|
@@ -1423,3 +1423,55 @@ void wxUsleep(unsigned long milliseconds);
|
|||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
|
||||||
|
/** @addtogroup group_funcmacro_misc */
|
||||||
|
//@{
|
||||||
|
/**
|
||||||
|
Convert decimal integer to 2-character hexadecimal string.
|
||||||
|
|
||||||
|
@param dec
|
||||||
|
A number to be converted.
|
||||||
|
@param buf
|
||||||
|
A pointer to the buffer that receives hexadecimal string (not prefixed
|
||||||
|
by @c 0x). This buffer should be large enough to hold at least
|
||||||
|
3 characters: 2 hexadecimal digits and the terminating null character.
|
||||||
|
|
||||||
|
@remarks
|
||||||
|
Returned string is composed of uppercase hexdecimal characters.
|
||||||
|
|
||||||
|
@header{wx/utils.h}
|
||||||
|
*/
|
||||||
|
void wxDecToHex(unsigned char dec, wxChar *buf);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Convert decimal integer to 2-character hexadecimal string.
|
||||||
|
|
||||||
|
@param dec
|
||||||
|
A number to be converted.
|
||||||
|
@return
|
||||||
|
String containing hexadecimal string, not prefixed by @c 0x,
|
||||||
|
composed of uppercase characters.
|
||||||
|
|
||||||
|
@header{wx/utils.h}
|
||||||
|
*/
|
||||||
|
wxString wxDecToHex(unsigned char dec);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns 2 characters of hexadecimal representation of a given number.
|
||||||
|
|
||||||
|
@param dec
|
||||||
|
A number to be converted.
|
||||||
|
@param ch1
|
||||||
|
Pointer to the variable that receives 1st hexadecimal character.
|
||||||
|
It must not be @NULL.
|
||||||
|
@param ch2
|
||||||
|
Pointer to the variable that receives 2nd hexadecimal character.
|
||||||
|
It must not be @NULL.
|
||||||
|
|
||||||
|
@remarks
|
||||||
|
Returned characters are uppercase.
|
||||||
|
|
||||||
|
@header{wx/utils.h}
|
||||||
|
*/
|
||||||
|
void wxDecToHex(unsigned char dec, char* ch1, char* ch2);
|
||||||
|
//@}
|
||||||
|
@@ -117,7 +117,7 @@
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
// Array used in DecToHex conversion routine.
|
// Array used in DecToHex conversion routine.
|
||||||
static const wxChar hexArray[] = wxT("0123456789ABCDEF");
|
static const char hexArray[] = "0123456789ABCDEF";
|
||||||
|
|
||||||
// Convert 2-digit hex number to decimal
|
// Convert 2-digit hex number to decimal
|
||||||
int wxHexToDec(const wxString& str)
|
int wxHexToDec(const wxString& str)
|
||||||
@@ -128,27 +128,25 @@ int wxHexToDec(const wxString& str)
|
|||||||
return wxHexToDec((const char*) buf);
|
return wxHexToDec((const char*) buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert decimal integer to 2-character hex string
|
// Convert decimal integer to 2-character hex string (not prefixed by 0x).
|
||||||
void wxDecToHex(int dec, wxChar *buf)
|
void wxDecToHex(unsigned char dec, wxChar *buf)
|
||||||
{
|
{
|
||||||
int firstDigit = (int)(dec/16.0);
|
wxASSERT_MSG( buf, wxS("Invalid argument") );
|
||||||
int secondDigit = (int)(dec - (firstDigit*16.0));
|
buf[0] = hexArray[dec >> 4];
|
||||||
buf[0] = hexArray[firstDigit];
|
buf[1] = hexArray[dec & 0x0F];
|
||||||
buf[1] = hexArray[secondDigit];
|
|
||||||
buf[2] = 0;
|
buf[2] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert decimal integer to 2 characters
|
// Convert decimal integer to 2 characters
|
||||||
void wxDecToHex(int dec, char* ch1, char* ch2)
|
void wxDecToHex(unsigned char dec, char* ch1, char* ch2)
|
||||||
{
|
{
|
||||||
int firstDigit = (int)(dec/16.0);
|
wxASSERT_MSG( ch1 && ch2, wxS("Invalid argument(s)") );
|
||||||
int secondDigit = (int)(dec - (firstDigit*16.0));
|
*ch1 = hexArray[dec >> 4];
|
||||||
(*ch1) = (char) hexArray[firstDigit];
|
*ch2 = hexArray[dec & 0x0F];
|
||||||
(*ch2) = (char) hexArray[secondDigit];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert decimal integer to 2-character hex string
|
// Convert decimal integer to 2-character hex string (not prefixed by 0x).
|
||||||
wxString wxDecToHex(int dec)
|
wxString wxDecToHex(unsigned char dec)
|
||||||
{
|
{
|
||||||
wxChar buf[3];
|
wxChar buf[3];
|
||||||
wxDecToHex(dec, buf);
|
wxDecToHex(dec, buf);
|
||||||
|
Reference in New Issue
Block a user