简体   繁体   English

无需预制语法将十六进制字符串转换为 integer 值

[英]Converting a hexadecimal string to integer value without pre-made syntax

I'm taking a computer organization class in college and I was tasked with writing a java program that takes a user inputted string, calls a function that converts said string into a hexadecimal integer, and then output the results. I'm taking a computer organization class in college and I was tasked with writing a java program that takes a user inputted string, calls a function that converts said string into a hexadecimal integer, and then output the results.

The kicker is I can't use any existing syntax that will do this.踢球者是我不能使用任何现有的语法来做到这一点。 for example, Integer.parseInt(__,16) or printf.例如,Integer.parseInt(__,16) 或 printf。 It all neds to be hardcoded.这一切都需要硬编码。

Now I'm not asking for you to do my homework for me, Just wanting to be put in the right direction.现在我不是要求你为我做功课,只是想朝着正确的方向前进。

So far, I've made this but can't seem to get the method created right:到目前为止,我已经做到了,但似乎无法正确创建方法:

import java.util.*;

public class Demo_Class {公共 class Demo_Class {

public static void main(String[] args) 
{
    Scanner AI = new Scanner(System.in);
    String str;
    
    System.out.println("Please input a hexadecimal number: ");
    str = AI.nextLine();
    
    converter(str);
}
    

public static int converter(String in)
{
    String New = new String();
    for(int i = 0; i<= in.length(); i++)
    {
        New += in.charAt(i);
        System.out.println(New + 316);
    }
    
    return 0;
}

} }

@WJS gave a good hint, I'd just like to add that the charAt() returns the char , which is encoded in ASCII. @WJS 给出了一个很好的提示,我只想补充一点, charAt()返回以 ASCII 编码的char

As you can see in the ASCII table , the characters AF have decimal values from 65 to 70, while 0-9 go from 48 to 57 so you'll need to use them to convert the ASCII characters to their intended value.正如您在ASCII 表中所见,字符 AF 的十进制值介于 65 到 70 之间,而 0-9 go 则介于 48 到 57 之间,因此您需要使用它们将 ASCII 字符转换为其预期值。

To do so, you can either get the decimal value of a character by casting to short like short dec = (short)in.charAt(i);为此,您可以通过转换为short来获取字符的十进制值,例如short dec = (short)in.charAt(i); , or directly use the characters like char current = in.charAt(i) - 'A' . ,或直接使用char current = in.charAt(i) - 'A'之类的字符。

With this in mind, all that's left is some calculation, I'll leave that as the homework.考虑到这一点,剩下的就是一些计算,我会把它作为家庭作业。 :) :)

Also:还:

  • you are looping one character more than needed, change the i <= in.length() to i < in.length() , since it's going from 0你比需要循环一个字符,将i <= in.length()更改为i < in.length() ,因为它从 0 开始
  • I don't know what that 316 "magic number" is, if it does mean something, declare a variable with a meaningful name, like:我不知道 316 “幻数”是什么,如果它确实意味着什么,请声明一个具有有意义名称的变量,例如:
final int MEANINGFUL_NAME = 316;

Consider this, lets says you have the hex value 1EC which in hex digits would be 1, E, C .考虑一下,假设您有十六进制值1EC ,十六进制数字为1, E, C In decimal they would be 1, 14, 12 .在十进制中,它们将是1, 14, 12

so set sum = 0.所以设置总和= 0。

  • sum = sum*16 + 1. sum is now 1 sum = sum*16 + 1. sum 现在是 1
  • sum = sum*16 + 14 sum is now 30 sum = sum*16 + 14 sum 现在是 30
  • sum = sum*16 + 12 sum is now 492 sum = sum*16 + 12 sum 现在是 492

So 492 is the answer.所以492就是答案。

If you have a string of 1EC you need to convert to characters and then convert those characters to the decimal equivalent of hex values.如果您有一个1EC字符串,则需要转换为字符,然后将这些字符转换为十六进制值的十进制等效值。

Try this on paper until you get the feel and then code it.在纸上试一试,直到你有感觉,然后编码。 You can check your results using the Integer method you mentioned.您可以使用您提到的Integer方法检查您的结果。

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

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