19

How do I build an escape sequence string in hexadecimal notation.

Example:

string s = "\x1A"; // this will create the hex-value 1A or dec-value 26

I want to be able to build strings with hex-values between 00 to FF like this (in this example 1B)

string s = "\x" + "1B"; // Unrecognized escape sequence

Maybe there's another way of making hexadecimal strings...

2
  • I don't think I understand - in this example you want to have a single character with the ansi code 26? Commented Nov 12, 2008 at 14:37
  • \x and \u both are escape sequences for unicode characters. '\u03A9' and '\x03A9' both print omaga (Ω) symbol on console output. Commented Oct 15, 2016 at 2:05

4 Answers 4

46

Please try to avoid the \x escape sequence. It's difficult to read because where it stops depends on the data. For instance, how much difference is there at a glance between these two strings?

"\x9Good compiler"
"\x9Bad compiler"

In the former, the "\x9" is tab - the escape sequence stops there because 'G' is not a valid hex character. In the second string, "\x9Bad" is all an escape sequence, leaving you with some random Unicode character and " compiler".

I suggest you use the \u escape sequence instead:

"\u0009Good compiler"
"\u0009Bad compiler"

(Of course for tab you'd use \t but I hope you see what I mean...)

This is somewhat aside from the original question of course, but that's been answered already :)

Sign up to request clarification or add additional context in comments.

Comments

24

You don't store hexadecimal values in strings.

You can, but it would just be that, a string, and would have to be cast to an integer or a byte to actually read its value.

You can assign a hexadecimal value as a literal to an int or a byte though:

Byte value = 0x0FF;
int value = 0x1B;

So, its easily possible to pass an hexadecimal literal into your string:

string foo = String.Format("{0} hex test", 0x0BB);

Which would create this string "126 hex test".

But I don't think that's what you wanted?

1 Comment

why 0x0BB is 126? 126 is 0x7E and 0x0BB is 187, right?
7

There's an '\u' escape code for hexadecimal 16 bits unicode character codes.

Console.WriteLine( "Look, I'm so happy : \u263A" );

Comments

0

The accepted answer contains the statement:

You don't store hexadecimal values in strings.

Often this is true.
The domain of problems that lends itself to storing weird often non-printable characters a string, is rather small. But the jobs skill "integration" seems to be on the rise, and that is a place where you may want to do what the OP suggests.

If you are processing files, you may see that there is a custom set of characters that need to be ignored within the data. In that case, storing non-printable characters (tab, escape, etc.) in a string can be the way to go.
I'll call these "ignored characters" white space even though a few in this example are printable, and the term white space often refers only to non-printable characters.
It is easier to write it that way.

For example you may have the requirement that the following are white space:

  • space(0x32), tab(\t or 0x9), non-breaking space(0xA0), underscore(_), and dash(-)

but that these are not white space:

  • new line, m-dash, and n-dash

That definition of white space will prevent you from using the built-in C# functions.
It will also prevent using most common libraries containing string functions (such as Extensions.cs).

readonly string whiteSpace = "\u0032\t\u00A0_-";
In this example you can use Contains against the preceding string to detect which characters are white space (using the definition given above or any other custom definition).

Loading those special characters into a string is an especially good choice if the users/business analyst have a history of changing the requirements during development and/or shortly before release.
(A string is easy to change, and requires minimal retesting against other systems)


I don't have permission to add a comment to the accepted answer, I guess because of its age. Given the increasing number of "integration" jobs which are appearing, this question and the point this answer makes are both relevant to a lot of developers today (15 years after it was asked).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.