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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:00:29+00:00 2026-06-17T02:00:29+00:00

Today I was just playing around for basic conversions from one base to another.

  • 0

Today I was just playing around for basic conversions from one base to another. I goggled some code for converting from hex to octal, and I noticed that it mostly uses intermediate conversion to either decimal or binary and then back to octal.Is it possible write my own function for converting hex string to octal string without using any intermediate conversion.Also I do not want to use inbuilt printf option like %x or %o. Thanks for your inputs.

  • 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-06-17T02:00:30+00:00Added an answer on June 17, 2026 at 2:00 am

    Of course it is possible. A number is a number no matter what numeric system it is in. The only problem is that people are used to decimal and that is why they understand it better. You may convert from any base to any other.

    EDIT: more info on how to perform the conversion.

    First note that 3 hexadecimal digits map to exactly 4 octal digits. So having the number of hexadecimal digits you may find the number of octal digits easily:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int get_val(char hex_digit) {
      if (hex_digit >= '0' && hex_digit <= '9') {
        return hex_digit - '0';
      } else {
        return hex_digit - 'A' + 10;
      }
    }
    void convert_to_oct(const char* hex, char** res) {
      int hex_len = strlen(hex);
      int oct_len = (hex_len/3) * 4;
      int i;
    
      // One hex digit left that is 4 bits or 2 oct digits.
      if (hex_len%3 == 1) {
        oct_len += 2;
      } else if (hex_len%3 == 2) { // 2 hex digits map to 3 oct digits
        oct_len += 3;
      }
    
      (*res) = malloc((oct_len+1) * sizeof(char));
      (*res)[oct_len] = 0; // don't forget the terminating char.
    
      int oct_index = oct_len - 1; // position we are changing in the oct representation.
      for (i = hex_len - 1; i - 3 >= 0; i -= 3) {
        (*res)[oct_index] = get_val(hex[i]) % 8 + '0';
        (*res)[oct_index - 1] = (get_val(hex[i])/8+ (get_val(hex[i-1])%4) * 2) + '0';
        (*res)[oct_index - 2] = get_val(hex[i-1])/4 + (get_val(hex[i-2])%2)*4 + '0';
        (*res)[oct_index - 3] = get_val(hex[i-2])/2 + '0'; 
        oct_index -= 4;
      }
    
      // if hex_len is not divisible by 4 we have to take care of the extra digits:
      if (hex_len%3 == 1) {
         (*res)[oct_index] = get_val(hex[0])%8 + '0';
         (*res)[oct_index - 1] = get_val(hex[0])/8 + '0';
      } else if (hex_len%3 == 2) {
         (*res)[oct_index] = get_val(hex[1])%8 + '0';
         (*res)[oct_index - 1] = get_val(hex[1])/8 + (get_val(hex[0])%4)*4 + '0';
         (*res)[oct_index - 2] = get_val(hex[0])/4 + '0';
      }
    }
    

    Also here is the example on ideone so that you can play with it: example.

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

Sidebar

Related Questions

I just started playing around with threading today and I ran into something that
I am a beginner in node.js (infact started just today). One of the basic
I just started playing around with WPF today, and I couldn't find the datagridview
I've been playing around with the pluto-test-framework today, and I'd like to get some
i was just playing around with the ternary operator in my c class today.
I've just today learned of asp.net mvc, and I'm wondering what is needed from
I decided to learn LISP today, and have been playing around with it for
I just started playing with Django today and so far am finding it rather
I was just quickly playing with blocks today and I came across the error:
I've just started playing with mongoose today after reading The Little MongoDB book. I'm

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.