C #: how to beat hexadecimal digits

Well, I am working on a program for playing cards, and I save the values ​​of the cards as hexadecimal digits. Here is the array:

public int[] originalCards = new int[54] { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x51 }; 

The first digit refers to the suit (1 = blades, 2 = clubs; .... 5 = jokers) The second digit refers to the card number (1 = ace, 5 = 5; 13 = K, etc.).

I would like to do something like the following:

Pseudocode:

  public int ReturnCard(int num) { int card = currentDeck[num]; int suit = card.firsthexdigit; int value = card.secondhexdigit; return 0; } 

code>

I don’t need a new method for working with ints, I just included it for clarity.

Does anyone know how to do this in C #?

Edit: Ok, I use bit offset as described in one of the answers. I can get the second digit (suit) just fine, but the first digit keeps coming out as "0". Any idea why?

Edit: edit: ok, now great. Thanks guys.

+4
source share
5 answers

Here is the answer using bit fields.

 struct { unsigned int suit:4; unsigned int value:4; } card = currentDeck[num]; int suit = card.suit; int value = card.value; 

You may need to add an int to populate as the first or last field for the correct assembly of bits. Bit fields are commonly used to access equipment, because hardware registers often combine multiple flags into one byte.

By the way, if you use bit shifting, you want to shift by the number of bits in a hexadecimal digit. One hexadecimal digit contains the values ​​0 - 15 or 0 - F, this requires 4 bits not 8. Therefore, this should be used:

 int suit = (card & 0xF0) >> 4; 
+3
source

You don't really understand the syntax, you just do a few simple bit manipulations.

 int card = currentDeck[num]; int suit = (card & 0xF0) >> 4; int value = card & 0x0F; 

Will do what you want.

+5
source

To answer the question about using 0xF0 and 0x0F in the example of bit shifting, what they do is bitwise I. When you execute card & 0xF0 what you do is two values, this leads to setting all bits except 4, you are interested in 0 . Example:

  0x48 01001000 0x48 01001000 &0x0F &00001111 &0xF0 &11110000 ----- -------- ---- -------- 0x08 00001000 0x48 01000000 >> 4 -------- 00000100 
+3
source

You may try

 int card = originalCards[1]; int suit = card /16; int value = card % 16; 
+1
source

Here is a working example:

 using System; namespace Test { class MainClass { static int[] currentDeck = new int[54] { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x51 }; static void printParts (int num) { int card = currentDeck[num]; int suit = (card & 0xF0) >> 4; int value = (card & 0x0F); Console.Out.WriteLine( String.Format ("Card: {0:x4}, ", card) + String.Format ("Suit: {0:x4}, ", suit) + String.Format ("Value: {0:x4}", value )); } public static void Main (string[] args) { printParts( 7 ); printParts( 18 ); printParts( 30 ); printParts( 48 ); } } } 

This causes the following:

 Card: 0018, Suit: 0001, Value: 0008 Card: 0026, Suit: 0002, Value: 0006 Card: 0035, Suit: 0003, Value: 0005 Card: 004a, Suit: 0004, Value: 000a 

I'm not sure why your top numbers are wrong.

+1
source

Source: https://habr.com/ru/post/1303762/


All Articles