简体   繁体   English

C#:如何移位十六进制数字

[英]C#: How to bit shift hexadecimal digits

Okay, I am working on a card playing program, and I am storing card values 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 = spades; 2 = clubs; .... 5 = Jokers) The second digit is the number of the card (1 = ace, 5 = 5; 13 = K, etc). 第一个数字指的是套装(1 =黑桃; 2 =俱乐部; .... 5 = Jokers)第二个数字是卡的编号(1 = ace,5 = 5; 13 = K等)。

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; }

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

I don't need a new method to work on ints, I just included it for clarity's sake. 我不需要一种新方法来处理整数,我只是为了清楚起见而将其包括在内。

Anybody know how to do this in C#? 有谁知道如何在C#中做到这一点?

Edit: Okay, I am using bit shifting as described in one of the answers. 编辑:好的,我正在使用其中一个答案中描述的位移。 I can get the second digit (the suit) just fine, but the first digit keeps coming out as '0'. 我可以得到第二个数字(西装)就好了,但第一个数字一直保持为'0'。 Any idea why? 知道为什么吗?

Edit:edit: okay, works fine now. 编辑:编辑:好的,现在工作正常。 Thanks guys. 多谢你们。

You're not really "parsing" as such, just doing some simple bit manipulation. 你不是真正“解析”,只是做一些简单的位操作。

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

Will do what you want. 会做你想做的。

Here's an 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 in int for padding as either the first or last field to line the bits up properly. 您可能需要在int中添加填充作为第一个或最后一个字段以正确排列位。 Bit fields are normally used to access hardware because hardware registers frequently pack multiple flags into a single byte. 位字段通常用于访问硬件,因为硬件寄存器经常将多个标志打包到单个字节中。

By the way if you use the bit shift, you want to shift by the number of bits in a hexadecimal digit. 顺便说一下,如果使用位移,则需要按十六进制数字的位数移位。 One hex digit holds values 0 - 15 or 0 - F, this requires 4 bits not 8. So this should be used: 一个十六进制数字保存值0 - 15或0 - F,这需要4位而不是8.所以这应该使用:

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

To answer your question about the use of 0xF0 and 0x0F in the bit shift example what they are doing is a bitwise AND . 要回答关于在位移示例中使用0xF00x0F ,他们正在做的是按位AND When you do card & 0xF0 what you are doing is anding the two values, this results in setting all bits except the 4 you are interested in to 0 . 当您执行card & 0xF0 ,您正在执行的操作是两个值,这会将除您感兴趣的4之外的所有位设置为0 Ex: 例如:

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

You can try 你可以试试

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

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 produces 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 upper digits are not correct. 我不确定为什么你的高位数不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM