linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Woods <kazrak+kernel@cesmail.net>
To: sapuglha@yahoo.com.br
Cc: Luciano Moreira - igLnx <lucianolnx@ig.com.br>,
	linux-c-programming@vger.kernel.org
Subject: Re: Newbie Question -> writing Hexadecimal values
Date: Thu, 25 Sep 2003 15:36:42 -0700	[thread overview]
Message-ID: <5.2.1.1.0.20030925145528.02e64a88@no.incoming.mail> (raw)
In-Reply-To: <20030925183510.239df26c.sapuglha@yahoo.com.br>

There is at least one bug in your code below:

At 9/25/2003 06:35 PM -0300, Sapuglha wrote:
>#define DecValFromHexaChar(c) ((c)>='A' && (c)<='F' ?'A'-(c)+10 :(c))
>         int hexaVal=0;


I think you mean:
#define DecValFromHexaChar(c) ((c)>='A' && (c)<='F' ?(c)-'A'+10:(c)-'0')
         char hexaVal=0;


Simple function to convert an arbitrary ASCII string from any numeric base 
(up to 36) to an unsigned binary:

int str2int(const char *str, const int base)
{
         int     result = 0;
         char    digit;
         assert(base>1);
         assert(str != NULL);
         while (digit = *str++) {
                 if (digit >= '0' && digit <= '9') digit -= '0';
                 else if (digit >= 'A' && digit <= 'Z') digit -= 'A'-10;
                 else if (digit >= 'a' && digit <= 'z') digit -= 'a'-10;
                 else assert(("invalid digit",0));
                 assert(digit<base);
                 result=result*base+digit;
         }
         return result;
}

int main(void) {        /* sample call */
         printf("%X\n", str2int("DeadBeef", 16));
         return 0;
}

P.S.  That's off the top of my head and there may be logic or other errors, 
but you probably get the gist of the code.

--
Jeff Woods <kazrak+kernel@cesmail.net> 



  reply	other threads:[~2003-09-25 22:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-25 21:11 Newbie Question -> writing Hexadecimal values Sapuglha
     [not found] ` <3F735CC4.5030507@ig.com.br>
2003-09-25 21:35   ` Sapuglha
2003-09-25 22:36     ` Jeff Woods [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-09-25 21:36 Sandro Dangui
2003-09-25 21:38 ` Sapuglha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5.2.1.1.0.20030925145528.02e64a88@no.incoming.mail \
    --to=kazrak+kernel@cesmail.net \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=lucianolnx@ig.com.br \
    --cc=sapuglha@yahoo.com.br \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).