0

I tried to search for answer and couldn't find a similar question. I have an array of chars, where i need to store in the array numbers. The numbers can only be one char (not a string or more the one index in the array)

for example, 3 will turn to 11 and then to a char (mostly gibberish)

Now I want to reverse the process and from the single char c, I want to present the number in decimal form. I tried number of things, but I don’t fully understand the bitwise operations to find the number. I would very appreciate your help!

8
  • 2
    int i = static_cast<int>(c);? Commented Jul 29, 2021 at 9:00
  • 2
    Can you show some sample input and expected output? Looks like the code you have just represent an integer and store them in a char Commented Jul 29, 2021 at 9:00
  • 2
    I think you are confused about values and their representations here. int has no "decimal form" or anything like that. It has a value, and that value can be represented using binary, decimal, hexadecimal, etc. The loop you present could be replaced with c = <source_int>; and you can do the same with another int variable. Commented Jul 29, 2021 at 9:01
  • why are you over-doing things when it already have a way ?? Commented Jul 29, 2021 at 9:09
  • @ME-ON1 Isn't this obvious? Because OP is still learning and missing the relation of internal and external representation. I believe we all had this Heureka moment more or less in the past... Commented Jul 29, 2021 at 9:25

1 Answer 1

2

The problem is that you mix up two different types, i.e., int and char. You cannot simply convert a binary representation of an int to a single char because they have a different number of bytes used to represent them. With a char, you can represent only 256 different numbers.

Below I post a code example that converts an int to a binary representation and from this back to an int.

  • decToBin(int val, int* binaryNum) gets a value that is converted from decimal to binary. binaryNum is an array to store the result.
  • binToDec(int* binaryNum) converts the binary representation to an int and returns it
  • clearBinNum(int* binaryNum) clears the binary representation wiht 0
  • printBinNumber(int* binaryNum) is a helper to print the binary representation
  • binToChar(int* binaryNum) converts a binary representation to a char and returns it
  • charToDec(char charNum) converts a char to a decimal representation using only bit-operations

#include <iostream>
using namespace std;

// This is the number of bits used to
// represent an variable of type int.
int bit_pos_count = sizeof(int) * 8;

// ---------------------------------------------------------- //
void
decToBin(int val, int* binaryNum) {
  int bit_pos = 0;
  while (val > 0) {
    if (val % 2 == 1)
      binaryNum[bit_pos] = 1;
    else
      binaryNum[bit_pos] = 0;
    val = val / 2; // integer division
    ++bit_pos;
  }
}

// ---------------------------------------------------------- //
void
clearBinNumber(int* binaryNum) {
  for (int i = 0; i < bit_pos_count; ++i)
    binaryNum[i] = 0;
}

// ---------------------------------------------------------- //
void
printBinNumber(int* binaryNum) {
  for (int i = bit_pos_count - 1; i >= 0; --i)
    cout << binaryNum[i];
}

// ---------------------------------------------------------- //
int
binToDec(int* binaryNum) {
  int result = 0;
  for (int i = bit_pos_count - 1; i >= 0; --i) {
    result = result << 1;
    if (binaryNum[i] == 1)
      result = result | 1;
  }
  return result;
}

// ---------------------------------------------------------- //
char
binToChar(int* binaryNum) {
  int  char_size = sizeof(char) * 8;
  char result    = 0;
  for (int i = char_size - 1; i >= 0; --i) {
    result = result << 1;
    if (binaryNum[i] == 1)
      result = result | 1;
  }
  return result;
}

// ---------------------------------------------------------- //
int
charToDec(char charNum) {
  int  char_size = sizeof(char) * 8;
  char result    = 0;
  int  compare   = 1;
  for (int i = 0; i < char_size; ++i) {
    if ((charNum & compare) != 0)
      result = result | compare;
    compare = compare * 2;
  }
  return result;
}

// ---------------------------------------------------------- //
int
main(int argc, char const* argv[]) {
  cout << bit_pos_count << endl;
  int* binaryNum = new int[bit_pos_count];

  // printable chars start at 33
  for (int i = 33; i < 66; i += 1) {
    decToBin(i, binaryNum);
    char charNum = binToChar(binaryNum);

    cout << "decimal:             " << i << endl;
    cout << "binary:              ";
    printBinNumber(binaryNum);
    cout << endl;
    cout << "decimal from binary: " << binToDec(binaryNum) << endl;
    cout << "char from binary:    " << charNum << endl;
    cout << "decimal from char:   " << charToDec(charNum) << endl;
    cout << "---" << endl;
  }

  delete[] binaryNum;
}

decimal:             33
binary:              00000000000000000000000000100001
decimal from binary: 33
char from binary:    !
decimal from char:   33
---
decimal:             34
binary:              00000000000000000000000000100010
decimal from binary: 34
char from binary:    "
decimal from char:   34
---
decimal:             35
binary:              00000000000000000000000000100011
decimal from binary: 35
char from binary:    #
decimal from char:   35
---
Sign up to request clarification or add additional context in comments.

3 Comments

@JohnFilleau: As char might be signed/unsigned only 0-128 is secure; before C++20, values outside this range (for conversion) would be implementation-defined...
@Jarod C++ Fundamental Types says "For every value of type unsigned char in range [0, 255], converting the value to char and then back to unsigned char produces the original value. (since C++11)". While the representation can be either the same as signed or unsigned char, it looks like a char is still able to represent the full unsigned char range of [0, 255].
@JohnFilleau: I think you are right. I had a look at an ASCII table containing 127 entries. But a char has a size of 1 byte, i.e. 8 bits. 2^8 = 256. I edited the text accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.