Click here to Skip to main content
Click here to Skip to main content

Hex Stream to ASCII Converter

, 31 Mar 2009
Rate this:
Please Sign up or sign in to vote.
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).

unsigned char 	arr[] = {"606162636465666768696a6b6c6d6e6f\
                         707172737475767778797a7b7c7d7e7f8081828384858687"};
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 Smile | :) .  

History   

N/A

License

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

About the Author

im_xboss
Team Leader
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberwanft31-Mar-09 11:05 
GeneralRe: My vote of 1 Pinmemberim_xboss3-Apr-09 0:02 
Question??? Pinmemberbasementman31-Mar-09 5:17 
AnswerRe: ??? Pinmemberim_xboss3-Apr-09 0:00 
GeneralRe: ??? Pinmemberbasementman6-Apr-09 4:06 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by im_xboss
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid