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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:47:37+00:00 2026-05-15T17:47:37+00:00

I wrote program to convert decimal to binary for practice purposes but i get

  • 0

I wrote program to convert decimal to binary for practice purposes but i get some strange output. When doing modulo with decimal number, i get correct value but what goes in array is forward slash? I am using char array for being able to just use output with cout <<.

// web binary converter: http://mistupid.com/computers/binaryconv.htm

#include <iostream>
#include <math.h>
#include <malloc.h> // _msize
#include <climits>
#define WRITEL(x) cout << x << endl;
#define WRITE(x) cout << x;

using std::cout;
using std::endl;
using std::cin;

char * decimalToBinary(int decimal);
void decimal_to_binary_char_array();

static char * array_main;

char * decimalToBinary(int decimal) // tied to array_main
{
   WRITEL("Number to convert: " << decimal << "\n");
   char * binary_array;
   int t = decimal,     // for number of digits
       digits = 0,      // number of digits
       bit_count = 0;   // total digit number of binary number
   static unsigned int array_size = 0;
   if(decimal < 0) { t = decimal; t = -t; }  // if number is negative, make it positive
   while(t > 0) { t /= 10; digits++; }       // determine number of digits
   array_size = (digits * sizeof(int) * 3);  // number of bytes to allocate to array_main
   WRITEL("array_size in bytes: " << array_size);
   array_main = new char[array_size];
   int i = 0;  // counter for number of binary digits
   while(decimal > 0)
   {
      array_main[i] = (char) decimal % 2 + '0';
      WRITE("decimal % 2 = " << char (decimal % 2 + '0') << "  ");
      WRITE(array_main[i] << "  ");
      decimal = decimal / 2;
      WRITEL(decimal);
      i++;
   }
   bit_count = i;
   array_size = bit_count * sizeof(int) + 1;
   binary_array = new char[bit_count * sizeof(int)];
   for(int i=0; i<bit_count+1; i++)
      binary_array[i] = array_main[bit_count-1-i];
   //array_main[bit_count * sizeof(int)] = '\0';
   //WRITEL("\nwhole binary_array: "); for(int i=0; i<array_size; i++) WRITE(binary_array[i]); WRITEL("\n");
   delete [] array_main;
   return binary_array;
}

int main(void)
{

   int num1 = 3001; 
           // 3001    = 101110111001
           // 300     = 100101100
           // 1000    = 1111101000
           // 1200    = 10010110000
           // 1000000 = 11110100001001000000
           // 200000  = 110000110101000000
   array_main = decimalToBinary(num1);
   WRITEL("\nMAIN: " << array_main);

   cin.get();
   delete [] array_main;
   return 0;
}

The output:

Number to convert: 3001

array_size in bytes: 48
decimal % 2 = 1  /  1500
decimal % 2 = 0  0  750
decimal % 2 = 0  0  375
decimal % 2 = 1  1  187
decimal % 2 = 1  /  93
decimal % 2 = 1  1  46
decimal % 2 = 0  0  23
decimal % 2 = 1  1  11
decimal % 2 = 1  1  5
decimal % 2 = 1  1  2
decimal % 2 = 0  1  1
decimal % 2 = 1  1  0

MAIN: 1111101/100/

What are those forward slashes in output (1111101/100/)?

  • 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-15T17:47:38+00:00Added an answer on May 15, 2026 at 5:47 pm

    It must be array_main[i] = (char) (decimal % 2 + '0'); (note the parentheses). But anyway, the code is horrible, please write it again from scratch.

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

Sidebar

Ask A Question

Stats

  • Questions 459k
  • Answers 459k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just call .to_json on a string and it will be… May 15, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer First problem is that it's TextBox not Textbox (C# is… May 15, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer There's nothing special about javascript, you can return it from… May 15, 2026 at 11:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.