Click here to Skip to main content
15,500,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a unicode that comes from database result. I want to convert that to normal to int but it fails as it is unicode format.

What I have tried:

string id = "\x1"; //\x1 is the value came from db

int processId = Int32.Parse(id);

This crashes in above line and gives this error"Additional information: Input string was not in a correct format"

So i would like to know how do we convert this in to normal string so that i can parse it to int value in C#
Updated 2-May-21 21:45pm

1 solution

"\x1" is not a .NET format for hex values: that would be "0x1".
You can do it, if you replace the backslash. Do note that backslash is an escape character in C# strings, so you need to use the "@" prefix to create a literal string containing it:
string prefixedHex = @"\x1";
int intValue = Convert.ToInt32(prefixedHex.Replace(@"\x", "0x"), 16);
Share this answer
Rocky_Bas 3-May-21 2:55am    
What is 16 in the code what kind of index is that
OriginalGriff 3-May-21 3:07am     CRLF
What do you think it means? Hint 1: What does the "x" in your input string indicate? Hint 2: You can use the official documentation to find out what parameters are and what they do. In this case, google "Convert.ToInt32" and look for a version that takes an integer as the second parameter to the first link it returns ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900