C# Build hexadecimal notation string

1,433

Solution 1

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?

Solution 2

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 :)

Solution 3

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

Console.WriteLine( "Look, I'm so happy : \u263A" );
Share:
1,433
I_code
Author by

I_code

Updated on July 09, 2022

Comments

  • I_code
    I_code almost 2 years

    I created a few projects but now I don't know how to view and also I don't see any menu bar! I used to have work space,what might be wrong? please help.

    EDIT : I already mentioned I am not even able to see menu bar! so, no window or no FILE dropdown also! the eclipse is just opening and i can add project and other save print button are disabled, apart from save,forward backward disabled buttons , I am not able to see anything?! what to do?

    Also actually I have installed two JRE's and I have eclipse and eclipse for java ee also on the same machine, i recently installed java ee eclipse and jre 1.7 for some requirement, earlier I used to work with just eclipse and jre 1.8. I am not even java developer, but for some course, i need to install 1.7 jre and eclipse java ee! Now what might be the problem?

    • virendrao
      virendrao over 9 years
      To view projects either you can use Project Explorer or Package Explorer. Click Window -> Show View -> Package Explorer
    • PurkkaKoodari
      PurkkaKoodari over 9 years
      Can you post a screenshot?
    • I_code
      I_code over 9 years
      i dont even have reputation to post a screenshot :( please help.