-2

I'm asking for help here after searching all around the web

I'm working on a school project, in Windows Forms, using managed C++. My problem is that I've got an Int32 value and I need to convert it to a binary value using System::Convert but I don't know how to use it properly.

I've tried doing this :

convertit = Int32::Parse(str);
static unsigned char ToByte(convertit);

Thank's for your help

wonko realtime
  • 503
  • 8
  • 20
Rameleu
  • 105
  • 10
  • @Rameleu it's really not clear what language you're targeting. Native C++ doesn't provide an Int32 class and my best guess would be that you're targeting managed C++. If that's the case, consider having a look at the docs here: http://msdn.microsoft.com/de-de/library/b3h1hf19%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 – wonko realtime Jan 05 '15 at 10:14
  • I suspect that you actually want to convert to a string containing the binary representation of the number, i.e. its representation in base 2. Read through the documentation for `System::Convert` until you find a `ToString` that fits. – molbdnilo Jan 05 '15 at 10:20
  • I've been searching for hours on the documentation, and I've accomplished nothing. Actually I've parsed a string (str) to an Int32, and now I want to convert this Int32 into a binary number. – Rameleu Jan 05 '15 at 10:23
  • @Rameleu It's still unclear what you're aiming for, especially because you're mentioning System::Convert but then talk about "binary number", because Int32 somehow already is a binary representation. Can you show an example of a complete conversion without code but just values, and then some of the examples you already tried? Anyway, maybe this helps: http://msdn.microsoft.com/en-us/library/System.BitConverter.GetBytes%28v=vs.110%29.aspx – wonko realtime Jan 05 '15 at 10:34
  • Well my project is I need to make a binary to decimal calculator (and decimal to binary calculator) I've made the binary to decimal part : if(this->radioButtonBin->Checked==true) { int nb = System::Convert::ToInt32(str, 2); this->textBoxClosed->Text=nb.ToString(); } And now I need to make the other part, but I don't know how to do it – Rameleu Jan 06 '15 at 07:17

2 Answers2

0

System::Convert can be used for both conversions since it supports base 2:

#include "stdafx.h"

using namespace System;
using namespace System::Diagnostics;

int main(array<System::String ^> ^args)
{
   // see http://msdn.microsoft.com/de-de/library/swz6z5ks%28v=vs.110%29.aspx
   int i = System::Convert::ToInt32("101", 2);
   Debug::Assert(i == 5);
   // see http://msdn.microsoft.com/de-de/library/14kwkz77%28v=vs.110%29.aspx
   System::String^ s2 = System::Convert::ToString(i, 10);
   Debug::Assert(s2 == "5");
   return 0;
}

Please note that microsoft doesn't mention the word "radix" in their documentation but it seems handy to know for further searches, see Positional notation - stackoverflow offers a lot when searching for "radix conversion" fe.

Generally, i'd recommend to always search for an example in another .net language if no cli example can be found.

wonko realtime
  • 503
  • 8
  • 20
0

I've found a solution. All I needed to do was

         // Binary button checked
         if(this->radioButtonBin->Checked==true)
         { 
         int nb = System::Convert::ToInt32(str, 2);
         this->textBoxClosed->Text=nb.ToString();
         }

         // Decimal button checked
         else
         {
         int nb = Convert::ToInt32(str, 10);
         String^ nb1 = System::Convert::ToString(nb, 2);
         this->textBoxClosed->Text=nb1;
         }
Rameleu
  • 105
  • 10
  • consider accepting a solution - also your own! You might have a look [here](http://blog.stackoverflow.com/2009/01/accept-your-own-answers/) and [here](http://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question) – wonko realtime Jan 06 '15 at 14:18