将十六进制字符转换为 MIPS 中的十进制等效值

发布于 2024-07-18 09:34:24 字数 97 浏览 6 评论 0原文

如何获取单个 ASCII 字符并将其转换为 MIP 中的十进制等效值?

我是否只需要满足一些条件即可从 ascii 代码中减去一定数量以使其成为十进制表示形式?

How do I take a single ASCII character and convert it to its decimal equivelant in MIPs?

Do I simply have to have some conditions to subtract a certain amount from the ascii code to make it its decimal representation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法少女 2024-07-25 09:34:24

应检查单个十六进制字符是否在

  • “0”到“9”(48 到 57)、
  • “A”到“F”(65 到 70)或
  • “a”到“f”(97 到 102) 范围内)。

其他任何内容都是错误。 如果它确实落入这些范围之一,请执行以下操作:

  • 减去 48(将“0”-“9”降低为 0-9)。
  • 如果它仍然大于 9,则减去 7(将“A”-“F”降低到 10-15)。
  • 如果它仍然大于 15,则减去 32(将 'a'-'f' 降低到 10-15)。

如果您确定非十进制数字的字符始终为大写,则可以跳过上面每个列表中的第三步,但不需要大量额外的代码来完成此操作。

A single hex character should be checked if it's in the range

  • '0' thru '9' (48 thru 57),
  • 'A' thru 'F' (65 thru 70), or
  • 'a' thru 'f' (97 thru 102).

Anything else is an error. If it does fall within one of those ranges, perform the following:

  • Subtract 48 (brings '0'-'9' down to 0-9).
  • If it's still greater than 9, subtract 7 (brings 'A'-'F' down to 10-15).
  • If it's still greater than 15, subtract 32 (brings 'a'-'f' down to 10-15).

If you're certain that the character will always be uppercase for the non-decimal digits, you can skip the third step in each of those lists above but it doesn't require a lot of extra code to do it.

孤芳又自赏 2024-07-25 09:34:24

这是 Pax 所写内容的简单实现(假设十六进制数字 - A 到 F 始终为大写)

文件 hextodec.c

#include <stdio.h>

/*
*Converts an ASCII char to its decimal equivalent.
*Returns -1 on error.
*
*/
extern int hextodec(char* c);

int main(int argc,char **argv){
        int i=0;
        char digits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','F'};

        for (;i<16;i++){
                printf("%c\t%d\n",digits[i],hextodec(digits+i));
        }
        return 0;
}

文件 hextodec.S

#include <mips/regdef.h>

/* int hextodec(char* c) 
 *  first (and only) argument is set in register a0.
 *  return value is set in register v0.
 *  function calling convention is ignored.
 */
        .text
        .globl hextodec
        .align 2
        .ent hextodec


hextodec:

        lbu     t0,0(a0)        #load byte from argument

        li      t1,0X30
        li      t2,0x39

        andi    t1,t1,0x000000ff #Cast to word for comparison.
        andi    t2,t2,0x000000ff

        bltu    t0,t1,ERROR     #error if lower than 0x30
        bgt     t0,t2,dohex     #if greater than 0x39, test for A -F

        addiu   t0,t0,-0x30     #OK, char between 48 and 55. Subtract 48.
        b       return

dohex:  li      t1,0x41
        li      t2,0x46

        andi   t1,t1,0x000000ff #Cast to word for comparison.
        andi   t2,t2,0x000000ff

        /*is byte is between 65 and 70?*/

        bltu    t0,t1,ERROR     #error if lower than 0x41
        bgt     t0,t2,ERROR     #error if greater than 0x46

ishex:  addiu   t0,t0,-0x37     #subtract 55 from hex char ('A'- 'F')
        b       return

ERROR:  addiu   t0,zero,-1      #return -1.

return: move    v0,t0           #move return value to register v0

        jr      ra
        .end    hextodec

测试运行

root@:~/stackoverflow# ./hextodec 
0       0
1       1
2       2
3       3
4       4
5       5
6       6
7       7
8       8
9       9
A       10
B       11
C       12
D       13
E       14
F       15
root@:~/stackoverflow# 

Here's a simplistic implementation of what Pax wrote (it assumes that hexadecimal digits - A to F are always upper case)

File hextodec.c

#include <stdio.h>

/*
*Converts an ASCII char to its decimal equivalent.
*Returns -1 on error.
*
*/
extern int hextodec(char* c);

int main(int argc,char **argv){
        int i=0;
        char digits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','F'};

        for (;i<16;i++){
                printf("%c\t%d\n",digits[i],hextodec(digits+i));
        }
        return 0;
}

File hextodec.S

#include <mips/regdef.h>

/* int hextodec(char* c) 
 *  first (and only) argument is set in register a0.
 *  return value is set in register v0.
 *  function calling convention is ignored.
 */
        .text
        .globl hextodec
        .align 2
        .ent hextodec


hextodec:

        lbu     t0,0(a0)        #load byte from argument

        li      t1,0X30
        li      t2,0x39

        andi    t1,t1,0x000000ff #Cast to word for comparison.
        andi    t2,t2,0x000000ff

        bltu    t0,t1,ERROR     #error if lower than 0x30
        bgt     t0,t2,dohex     #if greater than 0x39, test for A -F

        addiu   t0,t0,-0x30     #OK, char between 48 and 55. Subtract 48.
        b       return

dohex:  li      t1,0x41
        li      t2,0x46

        andi   t1,t1,0x000000ff #Cast to word for comparison.
        andi   t2,t2,0x000000ff

        /*is byte is between 65 and 70?*/

        bltu    t0,t1,ERROR     #error if lower than 0x41
        bgt     t0,t2,ERROR     #error if greater than 0x46

ishex:  addiu   t0,t0,-0x37     #subtract 55 from hex char ('A'- 'F')
        b       return

ERROR:  addiu   t0,zero,-1      #return -1.

return: move    v0,t0           #move return value to register v0

        jr      ra
        .end    hextodec

test run

root@:~/stackoverflow# ./hextodec 
0       0
1       1
2       2
3       3
4       4
5       5
6       6
7       7
8       8
9       9
A       10
B       11
C       12
D       13
E       14
F       15
root@:~/stackoverflow# 
软的没边 2024-07-25 09:34:24

是的,从 ASCII 值中减去 48 可能是最简单的。

Yes subtracting 48 from the ASCII value will probably be easiest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文