COnverting char to int and retaining value
-
The Below code was able to take 2 char's from an array stream and turn it into an integer # and Convert it to Decimal. However, unless I am missing something here, what it fails to do is the other half of what my problem was, that I wanted to take those 2 chars from the array stream and just convert them to an int value and leave them be, not convert them to decimal. I tried to do it myself, but it wasn't coming out correct. Thank You
#include #include using namespace std; int Convert(char a, char b); void main(){ cout << "" << Convert('A', 'A') << endl; } int Convert(char a, char b){ int intTemp, intVal = 0; char strBuffer[3]; strBuffer[0] = a; strBuffer[1] = '\0'; intTemp = atoi(strBuffer); if (intTemp != 0){ intVal = intTemp << 4; } else{ switch (toupper(a)){ case 'A': intVal = intVal | 0xA0; break; case 'B': intVal = intVal | 0xB0; break; case 'C': intVal = intVal | 0xC0; break; case 'D': intVal = intVal | 0xD0; break; case 'E': intVal = intVal | 0xE0; break; case 'F': intVal = intVal | 0xF0; break; default: return 0; break; } } strBuffer[0] = b; strBuffer[1] = '\0'; intTemp = atoi(strBuffer); if (intTemp != 0){ intVal = intVal | intTemp; } else{ switch (toupper(b)){ case 'A': intVal = intVal | 0x0A; break; case 'B': intVal = intVal | 0x0B; break; case 'C': intVal = intVal | 0x0C; break; case 'D': intVal = intVal | 0x0D; break; case 'E': intVal = intVal | 0x0E; break; case 'F': intVal = intVal | 0x0F; break; default: return 0; break; } } return intVal; }
-
The Below code was able to take 2 char's from an array stream and turn it into an integer # and Convert it to Decimal. However, unless I am missing something here, what it fails to do is the other half of what my problem was, that I wanted to take those 2 chars from the array stream and just convert them to an int value and leave them be, not convert them to decimal. I tried to do it myself, but it wasn't coming out correct. Thank You
#include #include using namespace std; int Convert(char a, char b); void main(){ cout << "" << Convert('A', 'A') << endl; } int Convert(char a, char b){ int intTemp, intVal = 0; char strBuffer[3]; strBuffer[0] = a; strBuffer[1] = '\0'; intTemp = atoi(strBuffer); if (intTemp != 0){ intVal = intTemp << 4; } else{ switch (toupper(a)){ case 'A': intVal = intVal | 0xA0; break; case 'B': intVal = intVal | 0xB0; break; case 'C': intVal = intVal | 0xC0; break; case 'D': intVal = intVal | 0xD0; break; case 'E': intVal = intVal | 0xE0; break; case 'F': intVal = intVal | 0xF0; break; default: return 0; break; } } strBuffer[0] = b; strBuffer[1] = '\0'; intTemp = atoi(strBuffer); if (intTemp != 0){ intVal = intVal | intTemp; } else{ switch (toupper(b)){ case 'A': intVal = intVal | 0x0A; break; case 'B': intVal = intVal | 0x0B; break; case 'C': intVal = intVal | 0x0C; break; case 'D': intVal = intVal | 0x0D; break; case 'E': intVal = intVal | 0x0E; break; case 'F': intVal = intVal | 0x0F; break; default: return 0; break; } } return intVal; }
CNewbie wrote: just convert them to an int value and leave them be, not convert them to decimal I don't understand what you're asking there. What's the expected behavior and how is it not working? --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.
-
CNewbie wrote: just convert them to an int value and leave them be, not convert them to decimal I don't understand what you're asking there. What's the expected behavior and how is it not working? --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.
The above code takes 2 chars from the array and converts them to an int value. However (and I proved this by running the code) it also converts the new int value from HEX to Decimal. That helped me in one situation, but another situation where I dont want to convert that new int value from HEX to Dec, but need it to stay a HEX Value it doesnt help. Hopefully someone understands what I am saying and can help. Thanks
-
The above code takes 2 chars from the array and converts them to an int value. However (and I proved this by running the code) it also converts the new int value from HEX to Decimal. That helped me in one situation, but another situation where I dont want to convert that new int value from HEX to Dec, but need it to stay a HEX Value it doesnt help. Hopefully someone understands what I am saying and can help. Thanks
You dont convert from hex to decimal, they are just different ways of representing the same thing. In your case, you are outputting an 'int' variable to cout. Unless you tell cout otherwise, it will always display this as a decimal. If you want to display it as hex, then you need:
cout << hex << myNumber;
-
The above code takes 2 chars from the array and converts them to an int value. However (and I proved this by running the code) it also converts the new int value from HEX to Decimal. That helped me in one situation, but another situation where I dont want to convert that new int value from HEX to Dec, but need it to stay a HEX Value it doesnt help. Hopefully someone understands what I am saying and can help. Thanks
You can mofify the code as follows: int Convert(char a, char b){ int intTemp=0; char strBuffer[3]; strBuffer[0] = a; strBuffer[1] = b; strBuffer[2] = '\0'; sscanf(strBuffer,"%2x",&intTemp); return intTemp; } This returns the HEX'ab' int value. The HEX or Decimal or Octal or whatever is just a representation of an integer number. What is exactly your problem?
-
You can mofify the code as follows: int Convert(char a, char b){ int intTemp=0; char strBuffer[3]; strBuffer[0] = a; strBuffer[1] = b; strBuffer[2] = '\0'; sscanf(strBuffer,"%2x",&intTemp); return intTemp; } This returns the HEX'ab' int value. The HEX or Decimal or Octal or whatever is just a representation of an integer number. What is exactly your problem?
itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow Convert an integer to a string. char *_itoa( int value, char *string, int radix ); char *_i64toa( __int64 value, char *string, int radix ); char * _ui64toa( unsigned _int64 value, char *string, int radix ); wchar_t * _itow( int value, wchar_t *string, int radix ); wchar_t * _i64tow( __int64 value, wchar_t *string, int radix ); wchar_t * _ui64tow( unsigned __int64 value, wchar_t *string, int radix ); Routine Required Header Compatibility _itoa Win 95, Win NT _i64toa Win 95, Win NT _ui64toa Win 95, Win NT _itow Win 95, Win NT _i64tow Win 95, Win NT _ui64tow Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a pointer to string. There is no error return. Parameters value Number to be converted string String result radix Base of value; must be in the range 2 – 36 Remarks The _itoa, _i64toa, and _ui64toa function convert the digits of the given value argument to a null-terminated character string and stores the result (up to 33 bytes) in string. If radix equals 10 and value is negative, the first character of the stored string is the minus sign ( – ). _itow, _i64tow, and _ui64tow are wide-character versions of _itoa, _i64toa, and _ui64toa respectively. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _itot _itoa _itoa _itow Example /* ITOA.C: This program converts integers of various * sizes to strings in various radixes. */ #include #include void main( void ) { char buffer[20]; int i = 3445; long l = -344115L; unsigned long ul = 1234567890UL; _itoa( i, buffer, 10 ); printf( "String of integer %d (radix 10): %s\n", i, buffer ); _itoa( i, buffer, 16 ); printf( "String of integer %d (radix 16): 0x%s\n", i, buffer ); _itoa( i, buffer, 2 ); printf( "String of integer %d (radix 2): %s\n", i, buffer ); _ltoa( l, buffer, 16 ); printf( "String of long int %ld (radix 16): 0x%s\n", l, buffer ); _ultoa( ul, buffer, 16 ); printf( "String of unsigned