Converting Entered TEXT into integer

 

My only apology is that I am a long time C programmer and I want to manipulate things directly. Now that said, someone please direct me in the correct way to analyze a TEXT string in visual C++.  Basically I want to read a string from a user enterable text window convert it into an integer and check it for proper range and send it to an embedded system in raw 16 bit form. My difficulty appears to be that there is no way to go direct from System::String^ (Textbox format) to an integer.  I would think that this is a common thing to do but my investigation leads me to some weird convoluted solutions.

This is how easy it ought to be:  

int myinteger;

myinteger = strtoi16(this->textBox1->Text);

or even

myinteger = this->textBox1->Text->strtoi16();

but apparently its not. Please help if you can.

Thanks

 

Bob



Answer this question

Converting Entered TEXT into integer

  • interscape

    I figured out how to convert to UInt32...

    UInt32 x = System::Convert::ToUInt32(textBox1->Text,16);

    This works, I have no idea if it's ideal.

    I figured out a way to convert UInt32 to Hex but it won't show leading zero's and you have to attach the "Microsoft.VisualBasic" reference in the properties page and then add the -- using namespace Microsoft::VisualBasic; -- to your file. Here is the code...

    textBox2->Text = Conversion::Hex(System::Convert::ToUInt32(x));

    Even though I'm not advanced enough to use Hexadecimals yet, please post if you find a better soultion.

     


  • Islandwind

    I have a .NET form with two textBox controls. I want to type in a 4 character string , in hex format, representing a 16 bit number  into  textBox1.  For example 000A ( leaving off the 0x portion of the hex number) I then want to convert the string into an unsigned int.  Once I have the unsigned int, I want to use it in some other function. 

     I need to display  in textBox2  a 4 digit hex format with leading zeros. Searching for help I came across your thread. Here is what I have:

    ----------------------------------

    unsigned int myint1;

    unsigned int myint2;

    myint1 = System::Convert::ToUInt16(textBox1->Text);

    myint2 = 0xdead;

    //textBox2->Text = String::Format("{0:X4}",myint2);

    ----------------------------------------------------------------

    When I enter non-decimal, for example 000a, into the textBox, I get an exception. This is my first stumbling block. How do I get the text into an unsigned int

    My second stumbling block is displaying the unsigned int in  textBox2 in hex format. I have it commented out so I can compile the first part. Format could not take in unsigned int.

     


  • Jacquipre

    if you are using .NET...

    int myinteger;

    myinteger = System::Convert::ToInt32(textBox1->Text);

    //ToInt32 can be ToInt16...etc, Intellisence will show you your choices...experiment.

    //The reverse is...

    textBox1->Text = String::Format("{0}",myinteger);


  • jbarton

    This works great for displaying hex format without the normal 0x hex qualifier.

    ----------------

    myint2 = 0x0A;

    textBox2->Text = myint2.ToString("X4");

    -------------------------

    I could not figure out how to add "Microsoft VisualBasic" reference to the properties page so I did not try teh VB solution. Which properties page do you add that to

    Also, I'll try to figure out how to use UInt16.Parse with the AllowHex specifier for my input.

    Thanks.


  • N B

    That worked great!

     

    Thanks,

     

    Bob


  • marvic

    1. ToString takes also a format argument. For hexadecimal its X or x

    2. UInt32.Parse allows to define a optional NumberStyle attribute were you can define also AllowHexSpecifier.

     



  • Peregrine Falcon

    Or using the built in ToString function of Int16!

  • Converting Entered TEXT into integer