如何将输入字符串用作十六进制 [英] How To use Input String as HeX

查看:133
本文介绍了如何将输入字符串用作十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在开发一个应用程序,其中用户将输入一个十六进制数和一个地址,应用程序已覆盖该地址的十六进制数.我面临的一个主要问题是,由于用户是通过控制台以 string 的形式提供值,因此我想使用相同的值,因为不需要进行转换,即如果用户给出了 0x9A567BFF 它将采用字符串形式,但我想与应用程序中的 hex 相同.请帮忙.

Hi All,


i am developing an application in which user will input a hexnumber and an address, application have override hexnumber at that address. One major issue i am facing that since user is giving value from console it is in the form of string, i want to use same value as no convertion is required ie if user gives 0x9A567BFF it will be in the form of string but i want to the same as hex in application. Please help.

推荐答案

StringToHexConvert.java

/**
*将字符串转换为十六进制十进制形式.
*/
导入java.io. *;

类StringToHexConvert {

字符串stringToHex(String str){
char [] chars = str.toCharArray();
StringBuffer strBuffer =新的StringBuffer();
for(int i = 0; i< chars.length; i ++){
strBuffer.append(Integer.toHexString((int)chars [i]));
}
return strBuffer.toString();
}

公共静态void main(String [] args)引发IOException {
System.out.print(输入字符串:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
字符串inputString = br.readLine();
StringToHexConvert obj = new StringToHexConvert();
字符串hexString = obj.stringToHex(inputString);
System.out.println(十六进制字符串:" + hexString);
}
}
程序输出:

输入字符串:mahendra
六进制形式的字符串:6d6168656e647261
StringToHexConvert.java

/**
* Convert a String to Hexa decimal form.
*/
import java.io.*;

class StringToHexConvert {

String stringToHex(String str) {
char[] chars = str.toCharArray();
StringBuffer strBuffer = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
strBuffer.append(Integer.toHexString((int) chars[i]));
}
return strBuffer.toString();
}

public static void main(String[] args) throws IOException {
System.out.print("Enter the String : ");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String inputString = br.readLine();
StringToHexConvert obj = new StringToHexConvert();
String hexString = obj.stringToHex(inputString);
System.out.println("String in hexa form : " + hexString);
}
}
Output of the program:

Enter the String: mahendra
String in hexa form: 6d6168656e647261


这篇关于如何将输入字符串用作十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆