Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:44:24+00:00 2026-05-26T08:44:24+00:00

Hello i try to write my own htoi method for convertion hexadecimal values to

  • 0

Hello i try to write my own htoi method for convertion hexadecimal values to a int values.

I stuck in.

    #include <stdio.h>
    #include <string.h>


    int main(int argc, char **argv)
    {
       int res = htoi2(argv[1]);
       fprintf(stdout, "%s => %d\n", argv[1], res);
       return 0;
     }


     int atoi2(char s[])
     {
        int i,n;
        n=0;
        for(i=0;s[i]>='0' && s[i]<='9';++i)
        {
           n=10*i+(s[i]-'0');                 
        }  
        return n; 

     }


  /*htoi(s)*/

   int htoi2(char s[])
   {       
   int i,n,len;
   n=0;
   len = strlen(s);
   for(i=0; i<len; i++)
   {
      if(s[i]>='0' &&s[i]<='9')
      {
         n=16*n+(s[i]-'0');
      }
      else if(s[i]>='a'&&s[i]<='f')
      {
          n=16*n+(s[i]-'a')+10;
      }
      else if(s[i]>='A'&&s[i]<='F')
      {
          n=16*n+(s[i]-'A')+10;
      }
  }
  return n;      
}

It seems to it should work but it doesn’t:(

anyone see some error in code i wrote?

Thanks for advance:)

Problem resolved

/*working code*/
int main(int argc, char **argv)
{
     char c[2];
     c[0]='F';
     c[1]='F';
     int res = htoi2(c);
     fprintf(stdout, "%d\n", res);
     system("pause");
     return 0;
}


 int atoi2(char s[])
 {
    int i,n;
    n=0;
    for(i=0;s[i]>='0' && s[i]<='9';++i)
    {
        n=10*i+(s[i]-'0');                 
    }  
    return n; 

 }


 /*htoi(s)*/

 int htoi2(char s[])
 {       
      int i,n,len;
      n=0;
      len = strlen(s);
      for(i=0; i<len; i++)
      {
         if(s[i]>='0' &&s[i]<='9')
         {
            n=16*n+(s[i]-'0');
         }
         else if(s[i]>='a'&&s[i]<='f')
         {
             n=16*n+(s[i]-'a')+10;
         }
         else if(s[i]>='A'&&s[i]<='F')
         {
             n=16*n+(s[i]-'A')+10;
         }
      }
      return n;      
}

Thanks for help 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:44:24+00:00Added an answer on May 26, 2026 at 8:44 am

    You’re looping the wrong way around.

        len = strlen(s);
        for(i=0; i<len; i++)
        {
           ...
    

    And your main is wrong. C-strings need to be 0-terminated. If you want to initialize it “manually”, you could do this:

        char c[3];
        c[0]='1';
        c[1]='1';
        c[2]=0;    // note: a real 0, not '0'
    

    Demo:

    #include <stdio.h>
    #include <string.h>
    
    int htoi(char s[])
    {
        int i,n,len;
        n=0;
        len = strlen(s);
        for(i=0; i<len; i++)
        {
            if(s[i]>='0' &&s[i]<='9')
            {
                n=16*n+(s[i]-'0');
            }
            else if(s[i]>='a'&&s[i]<='f')
            {
                n=16*n+(s[i]-'a')+10;
            }
            else if(s[i]>='A'&&s[i]<='F')
            {
                n=16*n+(s[i]-'A')+10;
            }
        }
        return n;
    }
    
    int main(int argc, char **argv)
    {
        int res = htoi(argv[1]);
        fprintf(stdout, "%s => %d\n", argv[1], res);
        return 0;
    }
    

    Does:

    $ gcc -Wall -m64 -o t t.c
    $ ./t 0
    0 => 0
    $ ./t 1
    1 => 1
    $ ./t a
    a => 10
    $ ./t f
    f => 15
    $ ./t 10
    10 => 16
    $ ./t 11
    11 => 17
    $ ./t 1a
    1a => 26
    $ ./t ff
    ff => 255
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I try to write a HTTP Request in C# (Post), but I need
hello all i`m php developer and i try to use new method in my
hello i got a problem, whenever i try to write any simple thing inside
FileWriter f; try { f = new FileWriter(Environment.getExternalStorageDirectory()+ /Text/o1+.txt); f.write(Hello World); f.flush(); f.close(); }
When I try this code: dict_a = dict_b = dict_c = {} dict_c['hello'] =
HttpRequestValidationException occurs when I try post when txtBulletin contains any HTML, like Hello<br />World
First off - hello, this is my first Stack Overflow question so I'll try
I have a C++ DLL with code like this: LogMessage( Hello world ); try
I'm attemping to write my own bootloader and i'm having issues with writing to
I am try to write inside html document. this is my javascript code: <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.