Converting a string value to int
-
Hi Experts, I am trying to convert a VB application to C# .net. I came across an enum which contained values as
stsAvailabilityBits = &HF, stsFormAccessAllowed = &H1, stsValidUser = &H2, stsSystemAvailable = &H4, stsPermissionGranted = &H8, stsInquiryOnlyBits = &HF0, stsFormInquiryOnly = &H10, stsUserInquiryOnly = &H20, stsSystemInquiryOnly = &H40, stsRecordLocked = &H80, stsShutdownMinutesBase = &H100, stsShutdownMinutes = &HF00, stsErrorReturned = &H1000, stsFatalError = &H2000, stsTableEmpty = &H4000, stsResultsReturned = &H8000, stsParentControl = &H10000, stsLoadResultset = &H20000, stsSpecialPrivBitsReturned = &H40000, stsRecordAccessReturned = &H80000, stsTableLockRequested = &H100000, stsTableLockConflict = &H200000, stsMultipleLockConflict = &H400000, stsAvailBit23 = &H800000, stsUserCanExport = &H1000000, stsUserCanRename = &H2000000, stsUserCanReport = &H4000000, stsUserCanDelete = &H8000000, stsGetForInquiry = &H10000000, stsWebUser = &H20000000, stsAvailBit30 = &H40000000, stsAvailBit31 = &H80000000
I am not able to understand what are these. I think they are hexadecimal values. If they are then how can i convert them into decimal/integer values in C#.net? Please help! Thanks! Regards, Samar -
Hi Experts, I am trying to convert a VB application to C# .net. I came across an enum which contained values as
stsAvailabilityBits = &HF, stsFormAccessAllowed = &H1, stsValidUser = &H2, stsSystemAvailable = &H4, stsPermissionGranted = &H8, stsInquiryOnlyBits = &HF0, stsFormInquiryOnly = &H10, stsUserInquiryOnly = &H20, stsSystemInquiryOnly = &H40, stsRecordLocked = &H80, stsShutdownMinutesBase = &H100, stsShutdownMinutes = &HF00, stsErrorReturned = &H1000, stsFatalError = &H2000, stsTableEmpty = &H4000, stsResultsReturned = &H8000, stsParentControl = &H10000, stsLoadResultset = &H20000, stsSpecialPrivBitsReturned = &H40000, stsRecordAccessReturned = &H80000, stsTableLockRequested = &H100000, stsTableLockConflict = &H200000, stsMultipleLockConflict = &H400000, stsAvailBit23 = &H800000, stsUserCanExport = &H1000000, stsUserCanRename = &H2000000, stsUserCanReport = &H4000000, stsUserCanDelete = &H8000000, stsGetForInquiry = &H10000000, stsWebUser = &H20000000, stsAvailBit30 = &H40000000, stsAvailBit31 = &H80000000
I am not able to understand what are these. I think they are hexadecimal values. If they are then how can i convert them into decimal/integer values in C#.net? Please help! Thanks! Regards, Samartry this code...
private string getDecodedKey(string strKey) { string strTemp = string.Empty; string\[\] strSplitKey = strKey.Split(' '); foreach (string strHex in strSplitKey) { try { int intVal = Convert.ToInt32(strHex, 16); strTemp = strTemp + Char.ConvertFromUtf32(intVal); } catch { strTemp = strTemp + strHex + "GET DECODED KEY"; } } return strTemp; }
-
Hi Experts, I am trying to convert a VB application to C# .net. I came across an enum which contained values as
stsAvailabilityBits = &HF, stsFormAccessAllowed = &H1, stsValidUser = &H2, stsSystemAvailable = &H4, stsPermissionGranted = &H8, stsInquiryOnlyBits = &HF0, stsFormInquiryOnly = &H10, stsUserInquiryOnly = &H20, stsSystemInquiryOnly = &H40, stsRecordLocked = &H80, stsShutdownMinutesBase = &H100, stsShutdownMinutes = &HF00, stsErrorReturned = &H1000, stsFatalError = &H2000, stsTableEmpty = &H4000, stsResultsReturned = &H8000, stsParentControl = &H10000, stsLoadResultset = &H20000, stsSpecialPrivBitsReturned = &H40000, stsRecordAccessReturned = &H80000, stsTableLockRequested = &H100000, stsTableLockConflict = &H200000, stsMultipleLockConflict = &H400000, stsAvailBit23 = &H800000, stsUserCanExport = &H1000000, stsUserCanRename = &H2000000, stsUserCanReport = &H4000000, stsUserCanDelete = &H8000000, stsGetForInquiry = &H10000000, stsWebUser = &H20000000, stsAvailBit30 = &H40000000, stsAvailBit31 = &H80000000
I am not able to understand what are these. I think they are hexadecimal values. If they are then how can i convert them into decimal/integer values in C#.net? Please help! Thanks! Regards, Samar -
Hi Experts, I am trying to convert a VB application to C# .net. I came across an enum which contained values as
stsAvailabilityBits = &HF, stsFormAccessAllowed = &H1, stsValidUser = &H2, stsSystemAvailable = &H4, stsPermissionGranted = &H8, stsInquiryOnlyBits = &HF0, stsFormInquiryOnly = &H10, stsUserInquiryOnly = &H20, stsSystemInquiryOnly = &H40, stsRecordLocked = &H80, stsShutdownMinutesBase = &H100, stsShutdownMinutes = &HF00, stsErrorReturned = &H1000, stsFatalError = &H2000, stsTableEmpty = &H4000, stsResultsReturned = &H8000, stsParentControl = &H10000, stsLoadResultset = &H20000, stsSpecialPrivBitsReturned = &H40000, stsRecordAccessReturned = &H80000, stsTableLockRequested = &H100000, stsTableLockConflict = &H200000, stsMultipleLockConflict = &H400000, stsAvailBit23 = &H800000, stsUserCanExport = &H1000000, stsUserCanRename = &H2000000, stsUserCanReport = &H4000000, stsUserCanDelete = &H8000000, stsGetForInquiry = &H10000000, stsWebUser = &H20000000, stsAvailBit30 = &H40000000, stsAvailBit31 = &H80000000
I am not able to understand what are these. I think they are hexadecimal values. If they are then how can i convert them into decimal/integer values in C#.net? Please help! Thanks! Regards, Samareither u can use escape sequence like char ch='\xFF'; Cheers
Rajesh B --> A Poor Workman Blames His Tools <--
-
try this code...
private string getDecodedKey(string strKey) { string strTemp = string.Empty; string\[\] strSplitKey = strKey.Split(' '); foreach (string strHex in strSplitKey) { try { int intVal = Convert.ToInt32(strHex, 16); strTemp = strTemp + Char.ConvertFromUtf32(intVal); } catch { strTemp = strTemp + strHex + "GET DECODED KEY"; } } return strTemp; }
Hi Ice, I tried running your code with the value of strKey as "&HF" and it showed me the error "Could not find any recognizable digits." on this line of code
int intVal = Convert.ToInt32(strHex, 16);
. Am i doing anything wrong here? Regards, Samar -
Hexadecimal, Decimal, Binary etc are representations, they're always good values. Replace "&H" by "0x". Cheers
I don't like my signature at all
Hi Estys,
Estys wrote:
Replace "&H" by "0x".
What am i suppose to do after replacing? Because even after replacing it wont be an integer or float or decimal, right?? Am i suppose to try and convert into an int or something after that?? Regards, Samar
-
Hi Estys,
Estys wrote:
Replace "&H" by "0x".
What am i suppose to do after replacing? Because even after replacing it wont be an integer or float or decimal, right?? Am i suppose to try and convert into an int or something after that?? Regards, Samar
(vb) &HFF = (c#) 0xFF = 255 The values are the same, it's just another way of representation. The only thing that matters is the type of the variable. So
int myint = 0xFF;
is perfectly legal. The following lines are equivalent integer declaration / assigments:int sixteen = 16;
int sixteen = 0x10;This is a float :
float onetwethreefourfive = 123.45f;
So you don't need to convert the value, just replace the hex-prefix from &H to 0x. In your case, some of the values cannot be assigned to
int
but must be assigned touint
orlong
, for instance0x80000000
.I don't like my signature at all
modified on Tuesday, April 27, 2010 5:28 AM
-
(vb) &HFF = (c#) 0xFF = 255 The values are the same, it's just another way of representation. The only thing that matters is the type of the variable. So
int myint = 0xFF;
is perfectly legal. The following lines are equivalent integer declaration / assigments:int sixteen = 16;
int sixteen = 0x10;This is a float :
float onetwethreefourfive = 123.45f;
So you don't need to convert the value, just replace the hex-prefix from &H to 0x. In your case, some of the values cannot be assigned to
int
but must be assigned touint
orlong
, for instance0x80000000
.I don't like my signature at all
modified on Tuesday, April 27, 2010 5:28 AM
Hey Estys, I got it! Thanks a lot! Just one more question. I have a textbox in which I am entering the data like "0xF". On click of a button i want to display its integer value. When i try the following code
Convert.ToInt32(txtHex.Text);
It is showing me "string not in correct format.". What am i missing here? Regards, Samar -
Hey Estys, I got it! Thanks a lot! Just one more question. I have a textbox in which I am entering the data like "0xF". On click of a button i want to display its integer value. When i try the following code
Convert.ToInt32(txtHex.Text);
It is showing me "string not in correct format.". What am i missing here? Regards, Samar -
That is good Estys. Thanks a million. Regards, Samar
-
try this code...
private string getDecodedKey(string strKey) { string strTemp = string.Empty; string\[\] strSplitKey = strKey.Split(' '); foreach (string strHex in strSplitKey) { try { int intVal = Convert.ToInt32(strHex, 16); strTemp = strTemp + Char.ConvertFromUtf32(intVal); } catch { strTemp = strTemp + strHex + "GET DECODED KEY"; } } return strTemp; }
Hi Ice, Your code was good except that i need to pass 0xF instead of &HF i.e. replacing &H with 0x. Also i am not getting the proper value when i use the following code but when i comment the same then it was working fine.
strTemp = strTemp + Char.ConvertFromUtf32(intVal);
Can you please let me know why were you using the above lines of code?? Regards, Samar
-
That is good Estys. Thanks a million. Regards, Samar
No it is not so good; it may preserve the value, however the readability is gone. These numbers look like special bit patterns, so they should remain in hex. All you need is a syntax change, from VB syntax (&H12) to C# syntax (0x12).
enum MyEnumType {
stsAvailabilityBits = 0xF,
stsFormAccessAllowed = 0x1,
...
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.