Thread: Signed hex string to a int

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Signed hex string to a int

    Im trying to write a program to parse a list of hex strings. Each hex string is 2 bytes and represents a two complements number. I need a fast and efficient way to convert this to a integer. Files containing the hex string are 30-100mb so i need something very efficient. if someone can help me out that would be great.

    THX

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A function that could convert hex to integer is strtol, I think.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    strtol() is probably the fastest way to do one number, as Elysia already suggested.

    However, if you know how many numbers to read and in what format, one of the family of scanf() function might be better.... but be aware of the issues of using scanf().

  4. #4
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    Quote Originally Posted by naruto267 View Post
    Im trying to write a program to parse a list of hex strings. Each hex string is 2 bytes and represents a two complements number. I need a fast and efficient way to convert this to a integer. Files containing the hex string are 30-100mb so i need something very efficient. if someone can help me out that would be great.
    Yep you have big files sizes. So what do you mean by being efficient? I feel that reading such big files will take bigger fractions of your time. So stop worrying about efficient converter function.

    /* Some one will be ready for knit picking. */

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe your emphasis on efficiency is mis-guided.

    If you haven't timed or profiled working code, how do you know you even have an efficiency problem?

    Your bottleneck should be your HD reading the data.

    In any case, show us your code where you have tried this, and let's see what's needed. Don't just drop the "I need a program to do this and that, and it really needs to be efficient, too", post on us, and expect a program served up on a silver platter.

    Our butler has retired.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    2
    Well i took this lookup table code form somewhere and modified it a bit
    Code:
    signed short parseHex(char *value)
    {
    	int i,j;
    	typedef struct
    	{
    		char chr;
    		signed short value;
    	} CHexMap;
    	int HexMapL = 16;
    	CHexMap HexMap[] = 
    	{
    		{'0', 0}, {'1', 1}, {'2', 2}, {'3', 3},
    		{'4', 4}, {'5', 5}, {'6', 6}, {'7', 7},
    		{'8', 8}, {'9', 9}, {'A', 10}, {'B', 11},
    		{'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}
    	};
    	signed short result = 0;
    	char *s = value;
    	for (j = 0; j < 4; j++)
    	{
    		result << 4;
    		for (i = 0; i < HexMapL; i++)
    	    {
    			if (*s == HexMap[i].chr)
    			{
    		        result |= HexMap[i].value;
    		        break;
    			}
    	    }
    		s++;
    	}
    	
    	return result;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM