0

i need a method like below

private byte GetByte(int decValue)
{
  //convert decValue to byte
}

Ex: if i give 10.. the function shud return 0X0A..wic shud be in byte format...

i know its simple...but i cant get my head work around it...

the application wic i'm working on has an RFID reader... the int values are the page(sector) number of the Tag, specified by the user... the reader manual specifically says we must send the value (0x0A) in byte format thru the serialport... ""Send an ‘R’ (=0x52) command to the reader to read pages 0‐3 from the tag’s memory: 0x52 0x00"" i hav to send 0x0A in byte format... but i cant ask client to learn hex n then tell them to enter 0x0A instead of 10 can i?

i've a function wic gives string "0x0A"... but i want it in byte format....

Anumia
  • 1
  • 5
  • 3
    I don't understand. `(byte) 0x0A` is the same as `(byte) 10` for the `byte` data type... do you want it to return the string `"0x0A"`? – BoltClock May 05 '11 at 11:32
  • Neither int nor byte are ever decimal or hex. This makes no sense at all. A string can be decimal or hex, integral types are just the abstract number without any representation specified. – CodesInChaos May 05 '11 at 11:54
  • the application wic i'm working on has an RFID reader... the int values are the page(sector) number of the Tag, specified by the user... the reader manual specifically says we must send the value (0x0A) in byte format thru the serialport... ""Send an ‘R’ (=0x52) command to the reader to read pages 0‐3 from the tag’s memory: 0x52 0x00"" i hav to send 0x0A in byte format... but i cant ask client to learn hex n then tell them to enter 0x0A instead of 10 can i? – Anumia May 06 '11 at 03:39

2 Answers2

1

Have a look at

How to convert numbers between hexadecimal and decimal in C#?

Community
  • 1
  • 1
FIre Panda
  • 6,537
  • 2
  • 25
  • 38
0

If you want to canvert an int value to hex representation you could use this

        int intVal = 10;
        string hexString = String.Format("{0:x2}", intVal);
        Console.WriteLine("hex value: {0}",hexString);
        Console.ReadLine();

but if you want to convert for int into byte type you will lose data because the int type has more than one byte size

Sergey K
  • 4,071
  • 2
  • 23
  • 34