Reputation: 45
Now that the question is answered, I will post my solution using the help that I got from everyone here (THANKS!)
The main thing I don't like is assuming every input ends in '\n' just because my linux terminal does! Also there has to be a more elegant way to convert A-F/a-f to decimal, maybe using a helper function that I write myself (don't have access to any functions yet besides pow and printf)!
I am doing K&R The C Programming Language, Exercise 2.3:
"Write a function, htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F."
#include <stdio.h>
#include <math.h>
#define MAXLEN 1000 /* maximum accepted user input is 999 digits */
int htoi( char s[], int maxPower ) ; /* maxPower will be two less than string length (len - 2) because the exponentiation is 0-indexed
* remove the length values which represent '\n' and '\0' in user input
* if the string begins with "0x" or "0X", we remove two more from the power variable inside the htoi() body. */
int main( void )
{
int c, i ;
int len ; /* tracks length of hexString, used to determine exponent value */
char hexString[ MAXLEN ] ;
len = 0 ;
for ( i = 0 ; i < MAXLEN - 1 && ( c = getchar() ) != EOF ; ++i ) {
hexString[i] = c ;
++len ;
}
hexString[len] = '\0' ; /* value of len is one more than termination value of i in above loop */
printf( "Hex String: %s\nInt: %d\n", hexString, htoi( hexString, len - 2 ) ) ;
return 0 ;
}
int htoi( char s[], int power )
{
int j, i, n, decimal ;
n = 0 ;
if ( s[0] == '0' && ( s[1] == 'x' || s[1] == 'X' ) ) { /* cutting off first two array values if string begins with 0x or 0X */
j = 0 ;
while ( ( s[j] = s[j + 2] ) != '\0' ) {
++j ;
}
power = power - 2 ; /* maximum exponent value now represents the usable data */
}
for ( i = 0 ; s[i] != '\n' && s[i] != '\0' ; ++i ) {
if ( s[i] >= '0' && s[i] <= '9' ) {
decimal = s[i] - '0' ;
n = ( decimal * pow( 16, power ) ) + n ;
--power ;
} else if ( s[i] == 'A' || s[i] == 'a' ) {
decimal = 10 ;
n = ( decimal * pow( 16, power ) ) + n ;
--power ;
} else if ( s[i] == 'B' || s[i] == 'b' ) {
decimal = 11 ;
n = ( decimal * pow( 16, power ) ) + n ;
--power ;
} else if ( s[i] == 'C' || s[i] == 'c' ) {
decimal = 12 ;
n = ( decimal * pow( 16, power ) ) + n ;
--power ;
} else if ( s[i] == 'D' || s[i] == 'd' ) {
decimal = 13 ;
n = ( decimal * pow( 16, power ) ) + n ;
--power ;
} else if ( s[i] == 'E' || s[i] == 'e' ) {
decimal = 14 ;
n = ( decimal * pow( 16, power ) ) + n ;
--power ;
} else if ( s[i] == 'F' || s[i] == 'f' ) {
decimal = 15 ;
n = ( decimal * pow( 16, power ) ) + n ;
--power ;
} else {
printf( "ERROR 69:\nInput string was not hexidecimal (0-9, A-F, a-f)\nResult is 0!\n" ) ;
n = 0;
return n;
}
}
return n ;
}
Upvotes: 3
Views: 25996
Reputation: 56
By string of hexadecimal digits what they mean is a combination of the digits 0-9 and characters A-F, just like how a binary string is a combination of 0's and 1's. Eg: "245FC" is a hexadecimal string.
Upvotes: 1
Reputation: 1828
Characters are represented as ASCII and they are 1 byte long, a byte could be represented with 2 digits in hexadecimal format for example 255 is 0xFF. What you will have to do is transform from hexa to charachters and then build your string.
Remember that to retrieve the ascii of a character you could do: int a = 'a'
Upvotes: 2
Reputation: 5721
What is meant is a string like AB0C5342F. In C terms:
char s[] = "AB0C5342F";
Upvotes: 3