C#:如何对十六进制数字进行位移位
好的,我正在开发一个纸牌游戏程序,我将纸牌值存储为十六进制数字。这是数组:
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
};
第一个数字表示花色(1 = 黑桃;2 = 梅花;.... 5 = 小丑) 第二位数字是牌号(1 = A,5 = 5;13 = K,等等)。
我想做如下的事情:
伪代码:
public int ReturnCard(int num)
{
int card = currentDeck[num];
int suit = card.firsthexdigit;
int value = card.secondhexdigit;
return 0;
}
我不需要新的方法来处理整数,我只是为了清楚起见而将其包含在内。
有人知道如何在 C# 中做到这一点吗?
编辑:好的,我正在使用答案之一中所述的位移位。我可以很好地得到第二个数字(花色),但第一个数字总是显示为“0”。知道为什么吗?
编辑:编辑:好的,现在工作正常。谢谢你们。
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).
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;
}
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#?
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'. Any idea why?
Edit:edit: okay, works fine now. Thanks guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您并没有真正“解析”,只是进行一些简单的位操作。
会做你想做的事。
You're not really "parsing" as such, just doing some simple bit manipulation.
Will do what you want.
为了回答您关于在位移示例中使用
0xF0
和0x0F
的问题,它们所做的是按位与。当你做卡& 0xF0
您所做的是对两个值进行与运算,这会导致将除您感兴趣的 4 之外的所有位设置为0
。前任:To answer your question about the use of
0xF0
and0x0F
in the bit shift example what they are doing is a bitwise AND. When you docard & 0xF0
what you are doing is anding the two values, this results in setting all bits except the 4 you are interested in to0
. Ex:这是使用位字段的答案。
您可能需要添加 int 进行填充,作为第一个或最后一个字段,以正确排列位。位字段通常用于访问硬件,因为硬件寄存器经常将多个标志打包到单个字节中。
顺便说一句,如果您使用位移位,您需要移动十六进制数字的位数。一个十六进制数字包含值 0 - 15 或 0 - F,这需要 4 位而不是 8 位。因此应该使用:
Here's an answer using bit fields.
You may need to add in int for padding as either the first or last field to line the bits up properly. 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:
你可以尝试
You can try
这是一个工作示例:
这会产生以下结果:
我不确定为什么您的上位数字不正确。
Here is a working example:
This produces the following:
I'm not sure why your upper digits are not correct.