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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:12:51+00:00 2026-05-26T22:12:51+00:00

Okay, so I have a question, I guess about C strings. Below is my

  • 0

Okay, so I have a question, I guess about C strings. Below is my modification to some code (given in an answer to a previous stackoverflow question!), with the function call and the output. The function converts an input hex number (of length 8) to a binary number (of length 32).

void htoi(const char *ptr, char *binAddr) {
char value[32] = "";
char ch = *ptr;
int i;
const char* quads[] = {"0000", "0001", "0010", "0011", "0100", "0101",
                     "0110", "0111", "1000", "1001", "1010", "1011",
                     "1100", "1101", "1110", "1111"};

while (ch == ' ' || ch == '\t')
    ch = *(++ptr);

for (i = 0; i < 8; i++) {
    if (ch >= '0' && ch <= '9')
        strncat(value, quads[ch - '0'], 4);
    if (ch >= 'A' && ch <= 'F')
        strncat(value, quads[10 + ch - 'A'], 4);
    if (ch >= 'a' && ch <= 'f')
        strncat(value, quads[10 + ch - 'a'], 4);

    ch = *(++ptr);
    printf("%s\n", value);
}

*binAddr = *value;
}

Here is my function call:

char line[11], hexAddr[8], binAddr[32];
htoi(hexAddr, binAddr);
printf("%s\n", binAddr);

Here is the output (when input with 001133c0):

0000

00000000

000000000001

0000000000010001

00000000000100010011

000000000001000100110011

0000000000010001001100111100

00000000000100010011001111000000

0���

The last line (with the special characters) is the printf(binAddr) in the main function above. It is clear from the printf statements inside the function that the binary code is being constructed correctly.

What am I doing wrong?

  • 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-26T22:12:51+00:00Added an answer on May 26, 2026 at 10:12 pm

    This line:

    *binAddr = *value;
    

    What do you think it does? Both arguments, before the asterisks are applied, refer to character arrays, or pointers to char. So when dereferenced, they refer to one char each. So this statement assigns the first char in binAddr equal to the first char in value, whereas presumably you wanted to return the whole string.

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

Sidebar

Related Questions

Okay I have the below code performing something I don't want it to do.
okay... I have asked this question about this but there is a new thing
Okay, so I have another question on a prolog homework problem I am struggling
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
Okay this may be a simple question but I have yet to come with
Okay guys, I have read through all the other posts and question on jQuery
Okay, it seems to be a stupid question, since we have this However, up
Okay I have updated my code quite a bit. I am getting a new
Earlier on i post a question about visualization and clustering. I guess my question
I have a question regarding nested doseq loops. In the start function, once I

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.