Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void UIntToHexStr(unsigned int uiValue, char cStr[]){
- unsigned int uiNibble;
- unsigned char ucNibbleCounter;
- cStr[0] = '0';
- cStr[1] = 'x';
- for(ucNibbleCounter = 0; ucNibbleCounter < 4; ucNibbleCounter++){
- uiNibble = ((uiValue >> (4 * ucNibbleCounter)) & 0xF);
- if(uiNibble < 10){
- cStr[5 - ucNibbleCounter] = uiNibble + '0';
- }
- else{
- cStr[5 - ucNibbleCounter] = uiNibble + 'A' - 10;
- }
- }
- cStr[6] = NULL;
- }
- enum Result eHexStringToUInt(char cStr[], unsigned int *puiValue){
- unsigned char ucLoopCounter;
- char cStrElement;
- *puiValue = 0;
- if((cStr[0] != '0') || (cStr[1] != 'x') || (cStr[2] == NULL)){
- return ERROR;
- }
- for(ucLoopCounter = 2; *(cStr + ucLoopCounter) != NULL; ucLoopCounter++){
- cStrElement = cStr[ucLoopCounter];
- if (ucLoopCounter > 5){
- return ERROR;
- }
- else if(cStrElement >= '0' && cStrElement <= '9'){
- *puiValue = (*puiValue << 4) + cStrElement - 48;
- }
- else if(cStrElement >= 'A' && cStrElement <= 'F'){
- *puiValue = (*puiValue << 4) + cStrElement - 55;
- }
- else{
- return ERROR;
- }
- }
- return OK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement