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 7502047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:44:13+00:00 2026-05-29T20:44:13+00:00

What would be the best way to go above converting from an integer array

  • 0

What would be the best way to go above converting from an integer array representing the bits of a number to hexadecimal?

My current attempt reads four bits at a time then tries to print the corresponding hex character, but I dont see to get any output.

Here is what I have:

 /**
  * Maps a 4 bit string in binary to the corresponding
  * hex digit
  */
 void map_bin_to_hex(char *c)
   {
      printf("In mapbin: C = %s\n", c); //This is printing funny output
      if(!strcmp(c, "0000"))      printf("0");
      else if(!strcmp(c, "0001")) printf("1");
      else if(!strcmp(c, "0010")) printf("2");
      else if(!strcmp(c, "0011")) printf("3");
      else if(!strcmp(c, "0100")) printf("4");
      else if(!strcmp(c, "0101")) printf("5");
      else if(!strcmp(c, "0110")) printf("6");
      else if(!strcmp(c, "0111")) printf("7");
      else if(!strcmp(c, "1000")) printf("8");
      else if(!strcmp(c, "1001")) printf("9");
      else if(!strcmp(c, "1010")) printf("A");
      else if(!strcmp(c, "1011")) printf("B");
      else if(!strcmp(c, "1100")) printf("C");
      else if(!strcmp(c, "1101")) printf("D");
      else if(!strcmp(c, "1110")) printf("E");
      else if(!strcmp(c, "1111")) printf("F");
   }

 /**
  * Reads 4 array elements at a time, passing them to map_bin_to_hex
  */
 void bin_array_to_hex(int *b, int length)
 {
    int i, j, k;

    //read 4 bits 16 times
      for(i = 0; i < 64; i+=4)
      {
         char hexB[4];
         for(k = 0, j = i; j < (i+4); j++, k++)
         {
            hexB[k] = b[j];
            printf("h[%d] = %d\n", k, hexB[k]);
         }
         map_bin_to_hex(hexB);
      }
      printf("\n");
  }

 int main()
 {
   // a 64-bit number represented as an array of bits
   int x[] =[1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,1,1,
             1,1,1,1,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,1,0];
   bin_array_to_hex(x, 64);
  }

The bin_array_to_hex() function reads the array in 4bit sections then passes those to map_bin_to_hex(), the output from bin_array_to_hex() is is correct, but something is wrong with my mapping function.

Any help would be appreciated.

EDIT: New Solution

   void map_bin_to_hex(char *c)
   {
        char hexMap[] = 
        {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

        int index = strtoull(c, NULL, 2);
        printf("%c", hexMap[index]);
   }
  • 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-29T20:44:15+00:00Added an answer on May 29, 2026 at 8:44 pm

    You are almost there,

    replace

    hexB[k] = b[j];
    

    with

    hexB[k] = b[j] + '0';
    

    Also, char hexB[4]; needs to be char hexB[5]; (last char is for null termination; don’t forget to set hexB[4]='\0'; before calling map_bin_to_hex).

    Once you’re done with this initial fix, take a look at other solutions available on the internet: you will see how to do the whole thing in five or six lines.

    EDIT As an additional challenge, see if you can use these two string constants to avoid the long chain of if-then-else statements in your first method:

    "0000000100100011010001010110011110001001101010111100110111101111"
    "0123456789abcdef"
    

    Hint: observe how the first string is exactly four times longer than the second one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to fill an array from user input? Would
What would be the best way to fill a C# struct from a byte[]
What would be the best way to determine if an object equals number zero
In Java, what would the best way be to have a constantly listening port
I want to make a code snippet database web application. Would the best way
What would be the best way to have a list of items with a
What would be the best way to implement a simple crash / error reporting
What would be the best way to calculate someone's age in years, months, and
What would be the best way to expose certain functionality in a Dotnet VSTO
What would be the best way to port an existing Drupal site to a

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.