Jitendra
Jitendra

Reputation: 323

In C# Is there any Datatype To Store the Hexadecimal Value?

In C#

I want to Check the value which is in 8 bit Binary format (i.e. 0000000a or 00010ef0) is between the specific range....

for example (following is C language code )

int temp=5;
if (temp>=0 || temp<10)
    printf("temp is between 0-10");

same way i want to check Hexadecimal value is in Given rage or not ....

Upvotes: 16

Views: 33156

Answers (4)

kamui
kamui

Reputation: 3419

You can convert the string of the hex value to an integer

int hexvalue = int.Parse(value.ToString(),System.Globalization.NumberStyles.HexNumber);

and then do your normal test

if (hexvalue >=0 || hexvalue <10)
    Console.WriteLine("hexvalue is between 0-10");

Upvotes: 4

Kieren Johnstone
Kieren Johnstone

Reputation: 42003

int temp=0x5; followed by if (temp >= 0xa || temp < 0x10ef0) looks like what you want.

That || means OR however, you probably want AND (&&).

Basically, prefix with 0x to tell the compiler you're specifying something in hex.

Upvotes: 11

Felice Pollano
Felice Pollano

Reputation: 33262

Except for the "printf" the code you post in C compile with the same effects in C#:

 int temp=5;
    if (temp>=0 || temp<10)
        Console.WriteLine("temp is between 0-10");

if you need to represent in C# an hexadecimal constant, prefix it with 0x, as you are used to do in C.

Upvotes: 3

KV Prajapati
KV Prajapati

Reputation: 94643

You may use int family types:

int val=0x19;

Upvotes: 8

Related Questions