Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Hex # into a value 1

Status
Not open for further replies.

redGSRguy

Programmer
Sep 16, 2005
12
0
0
US
Hi I am recently out of school and a pretty big noob when it comes to all this C++ builder stuff.

Right now I made an Edit box which the user is suppose to enter either a 8,6,4 or 2 digit Hex value. I basically want to store this as an integer so I can use the value later.

So far I am taking the ansistring converting it to a cstring, checking the value and converting it to hex based on it's position in the string. It seems to work ok. What I want to know is there some spiffy way to do this since my way seems pretty boring, also how would I go about error checking this to make sure they only entered the proper # of digit or to make sure they did not enter an invalid character. I was thinking maybe have a pop-up box displaying error then wiping the contents of the edit box to have them re-enter the value.

Thanks for any help guys, I have a ton of other question I need answered such as graying out radioboxes if a certain box is not checked and a bunch of other stuff to get my UI to work well. Hopefully these can be answered in the future.
 
Oh and I was using OnChange to call my convertToHex function but then I changed it too OnKeyDown checking for key = vk_return, as I saw in a post further down the page. Which one of these ways would be best to use for error checking. I thought the onkeydown would work best since I could check the entire string to make sure it was not too short/long and had appropriate values all at once.
 
I am not sure about your implementation so this may be
off base.

I dont see any reason for this

So far I am taking the ansistring converting it to a cstring, checking the value and converting it to hex based on it's position in the string.

the character "F" is already a representation of a hex value, there is no need to convert. you would just check, using a bunch of if else statements or a switch statement to check to see if the value of the char input was a valid hex char. i.e.

0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f

the default of the switch would be the error message

"Improper character value"

you could use isxdigit (). for example
Code:
#include <stdio.h>
#include <ctype.h>

int main(void)
{
  char c = 'C';

  if (isxdigit(c))
    printf("%c is a hexadecimal digit\n",c);
  else printf("%c is not a hexadecimal digit\n",c);

  return 0;
}

wereupon we would set the Key value in the onkeypress event to NULL.

you could also do a
Code:
if ((Edit1->Text.Lengthn ()/2 == 1)
 || (Edit1->Text.Lengthn ()/2 == 2) 
 || (Edit1->Text.Lengthn ()/2 == 3) 
 || (Edit1->Text.Lengthn ()/2 == 4))

{
    do this....
}

to check the length of the string.

the only conversion comes at the end when yu hit the enter key.

dont look so hard for spiffy. he will not teach you anything.


TC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top