[algogeeks] Re: Hexadecimal to Decimal

2011-09-02 Thread Gene
I'll add that both gcc and VC do a nice job of compiling this for x86: almostt identical code, 35 instructions in 88 bytes. An issue with the ctype.h functions - again only important in tight environments - is that some implementations use tables that are 1024 bytes. On Sep 1, 5:55 pm, Gene wrot

[algogeeks] Re: Hexadecimal to Decimal

2011-09-02 Thread Don
Sure. Is something wrong with using the functionality already built into the language? Here is another way: int hexToDec(char *string) { int x = 0; for(int i = 0; string[i]; ++i) { if (isdigit(string[i])) x = (x*16) + string[i] - '0'; else if ((string[i] >= 'a') && (string[i]

[algogeeks] Re: Hexadecimal to Decimal

2011-09-02 Thread Don
Sure there are other ways. But why duplicate functionality already built into the language? Here is one way to write your own conversion function: int hexToDec(char *string) { int x = 0; for(int i = 0; string[i]; ++i) { if (isdigit(string[i])) x = (x*16) + string[i] - '0'; els

[algogeeks] Re: Hexadecimal to Decimal

2011-09-01 Thread Gene
The language does give it tyou in sscanf, but sscanf is a pretty big function and in some environments, like small embedded ones, you don't get the luxury of using a big block of code to do a small thing. unsigned hex_to_unsigned(char *p) { unsigned val = 0; while (*p != '\0') { char c =

Re: [algogeeks] Re: Hexadecimal to Decimal

2011-09-01 Thread Yuchen Liao
Don's method is the best that I can think of. On Thu, Sep 1, 2011 at 1:24 PM, Don wrote: > Of course there are other methods, but why duplicate functionality > already provided by the language? > Don > > On Sep 1, 11:34 am, rShetty wrote: > > Given a Hexadecimal value as a string, give a C Code

[algogeeks] Re: Hexadecimal to Decimal

2011-09-01 Thread Don
Of course there are other methods, but why duplicate functionality already provided by the language? Don On Sep 1, 11:34 am, rShetty wrote: > Given a Hexadecimal value as a string, give a C Code to convert it > into decimal value? > If 0xff then output should be 255. -- You received this messag

[algogeeks] Re: Hexadecimal to Decimal

2011-09-01 Thread Don
int n; char *string = "0xff"; // Or whatever sscanf(string, "%x", &n); printf("%d\n", n); On Sep 1, 11:34 am, rShetty wrote: > Given a Hexadecimal value as a string, give a C Code to convert it > into decimal value? > If 0xff then output should be 255. -- You received this message because you

[algogeeks] Re: Hexadecimal to Decimal

2011-09-01 Thread Don
sscanf(string, "%x", &n); On Sep 1, 11:34 am, rShetty wrote: > Given a Hexadecimal value as a string, give a C Code to convert it > into decimal value? > If 0xff then output should be 255. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To