• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How do I tell my program that 5FF is in hex?!

uCsDNerd

Senior member
Hi guys. How the hecks do I make a C program understand that '5FF' is a hex number, not a string???

I've tried atoi(5FF) ... that doesn't seem to work.

Anyone help would be appreciated. thanks.
 
If you just want to assign a hex value to an int or a long, then prefix it with 0x, for example:

long lFoo;
lFoo = 0x5FF;

If, as your question implies, you want to convert a string (null terminated character array) to a value, interpreting the characters within it as hex digits, you can use either of strtol() or sscanf()...your compiler and libraries may provide other possibilities, but it is certain that sscanf() will be there. For example:

char* pStr = "5FF"; // Text string
char* pAfter;
long lFoo = strtol( pStr, &pAfter, 16 ); // Converts string to long using base 16 (hex), returns value
long lFoo2;
sscanf( pStr, "%lx", &lFoo2 ); // Stores converted value in lFoo2

 
awesome. thanks for the help code jockey. but i've got one more question for you guys....

about the hex number. I understand that %lx is a hex representation of a number, but say I want to pass this hex number to a function of mine. How would it be declared in the function parameters?

i.e -
long(<~?) hexNumber = 0x5FF
returnVal = foo( hexNumber ) // function call.
..
..
}

void foo( int? hexNumber ) { // function foo. <- do i declare it as an int? / double? / char? or what?
...
}

Main reason being is that I want to perform a bitwise operation on the hex number and I gotta make sure I perform it correctly. Thank you very much ! 🙂
 

You declare the function to be returning long, and to take a long as a parameter, then, inside your function do whatever you want to the long value and return it. For example, where you have:



<< long(<~?) hexNumber = 0x5FF
returnVal = foo( hexNumber ) // function call.
..
..
}

void foo( int? hexNumber ) { // function foo. <- do i declare it as an int? / double? / char? or what?
...
}
>>



I would suggest changing it to:

long hexNumber = 0x5FF;
long returnVal; // Declare variable returnVal to be a long integer

returnVal = foo( hexNumber ); // function call, takes a long, and returns a long
..
..
}

// Function foo()
// Takes a long integer (hexNumber) as a parameter
// and returns a long integer to the caller
long foo( long hexNumber )
{ // function foo...do what you want to do
long modifiedHexNumber = hexNumber >> 4; // Example, Shift input 4 bits to right
return modifiedHexNumber;
}


Hope this helps...

BTW, %lx is not a hex number, it is the tag that you put into the format string of a printf() (or sprintf, etc.) function to indicate that a long integer value should be expected in the list of arguments, and that it should be formatted into the output generated by printf() as a series of hex digits that represent the value.
 
Back
Top