如何将十六进制字符串转换为二进制?

问题描述 投票:2回答:2

我的问题是从用户那里获得64位密钥。为此,我需要获取16个字符作为包含十六进制字符(123456789ABCDEF)的字符串。我从用户那里得到了字符串,并用下面的代码找到了字符。但我不知道如何将字符转换为4位二进制

.data 


insert_into: 
    .word 8 

Ask_Input:  
    .asciiz "Please Enter a Key which size is 16, and use hex characters : " 


key_array: 
    .space 64

.text
.globl main


main: 

    la $a0, Ask_Input 

    li $v0, 4 
    syscall 

    la $a0, insert_into 
    la $a1, 64 
    li $v0, 8
    syscall

    la $t0, insert_into
    li $t2, 0
    li $t3, 0
  loop_convert:
        lb $t1, ($t0)
        addi $t0, $t0, 1 

        beq $t1, 10, end_convert

# Now charcter is in $t1 but 
  #I dont know how to convert it to 4 bit binary and storing it


        b loop_convert

    end_convert:        

    li $v0, 10  # exit
    syscall
input hex mips data-conversion spim
2个回答
-1
投票

[查看此ASCII table,您将看到9及以下的数字的十六进制代码为0x9,对于大写字母,此代码位于0x41和0x5A之间。AZ确定它是数字还是字符,看看是否有数字如果它是一个具有0x15的字符掩码以获取这四个位,则已经完成。

如果要包含小写字母,请执行相同的屏蔽操作,并确定其字符是否介于0x61和0x7A之间


0
投票

我不认为用0x15进行掩盖是正确的解决方案,因为@Joelmob认为,因为

'A' = 0x41 → 0x41 & 0x15 = 0
'B' = 0x42 → 0x42 & 0x15 = 0
'C' = 0x43 → 0x43 & 0x15 = 1
'D' = 0x44 → 0x44 & 0x15 = 4
'E' = 0x45 → 0x45 & 0x15 = 5
'F' = 0x46 → 0x46 & 0x15 = 4

不会产生任何相关的二进制值

最简单的方法是从字符值中减去范围的下限。我将用C给出想法,您可以轻松地将其转换为MIPS asm

if ('0' <= ch && ch <= '9')
{
    return ch - '0';
}
else if ('A' <= ch && ch <= 'F')
{
    return ch - 'A' + 10;
}
else if ('a' <= ch && ch <= 'f')
{
    return ch - 'a' + 10;
}

另一种实现方式:

if ('0' <= ch && ch <= '9')
{
    return ch & 0x0f;
}
else if (('A' <= ch && ch <= 'F') || ('a' <= ch && ch <= 'f'))
{
    return (ch & 0x0f) + 9;
}

但是可以使用以下问题中描述的技术将其进一步优化为单个比较

现在,支票可被重写如下

if ((unsigned char)(ch - '0') <= ('9'-'0'))
if ((unsigned char)(ch - 'A') <= ('F'-'A'))
if ((unsigned char)(ch - 'a') <= ('f'-'a'))

任何现代编译器都可以进行这种优化,here is an example output

hex2(unsigned char):
        andi    $4,$4,0x00ff     # ch, ch
        addiu   $2,$4,-48      # tmp203, ch,
        sltu    $2,$2,10         # tmp204, tmp203,
        bne     $2,$0,$L13
        nop
        andi    $2,$4,0xdf       # tmp206, ch,
        addiu   $2,$2,-65      # tmp210, tmp206,
        sltu    $2,$2,6  # tmp211, tmp210,
        beq     $2,$0,$L12         #, tmp211,,
        andi    $4,$4,0xf        # tmp212, ch,

        j       $31
        addiu   $2,$4,9        # D.2099, tmp212,

$L12:
        j       $31
        li      $2,255                  # 0xff   # D.2099,

$L13:
        j       $31
        andi    $2,$4,0xf        # D.2099, ch,
© www.soinside.com 2019 - 2024. All rights reserved.