Click here to Skip to main content
15,884,836 members
Articles / Programming Languages / C
Article

Hex Stream to ASCII Converter

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
31 Mar 2009CPOL1 min read 37.7K   680   10   5
This topic deals with the method of converting a stream of hex values to its corresponding ASCII values

Introduction 

The code presented here is used to convert a stream of hex values coming from the network to the corresponding ASCII values. Each 4 bits of a hex value is coming in its corresponding 8 bit format in the stream.

Background

We were working on a project in which we were getting a stream of data that was actually a Hex stream. Now every character in the stream was actually representing only 4 bits of the actual hex value. So 2 characters were required to form the actual hex value. I had checked the internet for a quick solution but was not very happy with what I saw. So I decided to write on of my own. I would not say this is the best solution. But it is fast enough.

Using the code

The code can be used by passing the character array arr to the function hexToAscii(). Here, the 2 different characters are actually 2 nibbles of a hex value. so 606162 are actually decoded as 0x60, 0x61, 0x62 and so on. The result is stored in an integer array destElements.

srcElements is the size of the array being passed as the input parameter (here arr).

C++
unsigned char 	arr[] = {"606162636465666768696a6b6c6d6e6f\
                         707172737475767778797a7b7c7d7e7f8081828384858687"};
C++
char *hexToAscii (char *srcHex, int *destElements, int srcElements)
{
    int		index, destIndex;
    unsigned char	valUpper, valLower, *ascArray;
    int		lowerFlag;
	
    lowerFlag = 0;
    ascArray = NULL;
    *destElements = srcElements/2;
    ascArray = (char *)malloc (*destElements);
    if (ascArray == NULL) {
        return NULL;
    }
    for (destIndex = 0; destIndex < *destElements; destIndex++) {
        //  Every hex value will be 2 nibbles long and will take up 2 character
        //  space in the character array (Input)
        for (index = 0; index < 2; index++) {
            if (lowerFlag == 0) {
                if (srcHex[(destIndex * 2) + index] >= '0' && 
                    srcHex[(destIndex * 2) + index] <= '9') {
                    valUpper = srcHex[(destIndex * 2) + index] - '0';
                } else if (tolower (srcHex[(destIndex * 2) + index]) >= 'a' || 
                           tolower (srcHex[(destIndex * 2) + index]) <= 'f'){
                    valUpper = tolower(srcHex[(destIndex * 2) + index]) - 'a' + 10;
                }
                //  The first character represent the upper nibble (upper 4 bits)
                //  of the hex value. Shift the converted value left 4 bits
                valUpper <<= 4;
                lowerFlag = 1;
            } else {
                if (srcHex[(destIndex * 2) + index] >= '0' && 
                    srcHex[(destIndex * 2) + index] <= '9') {
                    valLower = srcHex[(destIndex * 2) + index] - '0';
                } else if (tolower (srcHex[(destIndex * 2) + index]) >= 'a' || 
                           tolower (srcHex[(destIndex * 2) + index]) <= 'f'){
                    valLower = tolower (srcHex[(destIndex * 2) + index]) - 'a' + 10;
                }  
                lowerFlag = 0;
           }
	}
       //  ANDing upper and lower values will get the proper 8 bit ASCII converted val
       ascArray[destIndex] = valUpper | valLower;
    }
    return ascArray;
}

Points of Interest 

It was just fun coding this :-).  

History   

N/A

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
wanft31-Mar-09 11:05
wanft31-Mar-09 11:05 
GeneralRe: My vote of 1 Pin
im_xboss3-Apr-09 0:02
im_xboss3-Apr-09 0:02 
Question??? Pin
basementman31-Mar-09 5:17
basementman31-Mar-09 5:17 
AnswerRe: ??? Pin
im_xboss3-Apr-09 0:00
im_xboss3-Apr-09 0:00 
GeneralRe: ??? Pin
basementman6-Apr-09 4:06
basementman6-Apr-09 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.