几乎很少写JAVA代码,第一是确实不会,第二感觉JAVA写起来不爽(较python、golang),但总有万不得已必须要用java的时候。这里记录下使用java实现的hex十六进制和acsii码之间的转换(代码主要还是从网上找来的,简单改吧改吧)。

一、ASCII to Hex

这里是将ascii码转换为十六进制值,代码如下:

private static String asciiToHex(String asciiStr) {
char[] chars = asciiStr.toCharArray();
StringBuilder hex = new StringBuilder();
for (char ch : chars) {
hex.append(Integer.toHexString((int) ch));
}
return hex.toString();
}

中间使用十进制进行了转换一下。

二、hex to ascii

代码如下:

private static String hexToAscii(String hexStr) {
StringBuilder output = new StringBuilder("");
for (int i = 0; i < hexStr.length(); i += 2) {
String str = hexStr.substring(i, i + 2);
output.append((char) Integer.parseInt(str, 16));
}
return output.toString();
}

其流程是“Hex<==>Decimal<==>ASCII“。

三、完整示例
public class StringToHex{
public String convertStringToHex(String str){
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for(int i = 0; i < chars.length; i++){
hex.append(Integer.toHexString((int)chars[i]));
}
return hex.toString();
}
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for( int i=0; i<hex.length()-1; i+=2 ){
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char)decimal);
temp.append(decimal);
}
System.out.println("Decimal : " + temp.toString());
return sb.toString();
}
public static void main(String[] args) {
StringToHex strToHex = new StringToHex();
System.out.println("\n***** Convert ASCII to Hex *****");
String str = "My site is www.361way.com,Fucking Java!";
System.out.println("Original input : " + str);
String hex = strToHex.convertStringToHex(str);
System.out.println("Hex : " + hex);
System.out.println("\n***** Convert Hex to ASCII *****");
System.out.println("Hex : " + hex);
System.out.println("ASCII : " + strToHex.convertHexToString(hex));
}
}

上面的代码执行后,输出如下:

[root@localhost tmp]# java StringToHex
***** Convert ASCII to Hex *****
Original input : My site is www.361way.com,Fucking Java!
Hex : 4d792073697465206973207777772e3336317761792e636f6d2c4675636b696e67204a61766121
***** Convert Hex to ASCII *****
Hex : 4d792073697465206973207777772e3336317761792e636f6d2c4675636b696e67204a61766121
Decimal : 77121321151051161013210511532119119119465154491199712146991111094470117991071051101033274971189733
ASCII : My site is www.361way.com,Fucking Java!

看到上面的示例,是不是想到上面的代码的一个应用场景 ---- 密码简单加密。

本文转自:https://www.linuxprobe.com/java-hex-asci.html

使用java实现hex和ascii码的转换的更多相关文章

  1. java map里面进行ASCII 码从小到大排序(字典序)

    public static String getAsciiSort(Map<String, Object> map) { List<Entry<String, Object&g ...

  2. js中ascii码的转换

    今天在把原来用C写的程序移植到javascript上,但是有个地方一直调不通,后来才发现是js奇葩的字符处理出的问题.c中使用的字符处理比如加上一个字符值强制转换一下,在js中就行不通了. 但是js提 ...

  3. C#字符串和ASCII码的转换

    //字符转ASCII码: public static int Asc(string character) { if (character.Length == 1) { System.Text.ASCI ...

  4. C# ASCII码的转换、转义字符、对照表

    var splitStr = new byte[] { 0x05, 0x0D, 0x0A };//var splitStr = new byte[] { 5, 13, 10 };这样写也可以 var ...

  5. 将大量ASCII码值转换成字符函数CHR()的小技巧

    一.在学习命令执行漏洞的过程中,遇到以下情况: 当服务器上传不了马或者马被过滤的时候,我们可以迂回一下,通过执行写马命令到服务器,在服务器里面写马,该命令是通过ascii编码过的命令,防止被过滤. 1 ...

  6. 字符串/16进制/ASCII码的转换

    1 /// <字符串转16进制格式,不够自动前面补零> 2 /// 假设文本框里面填写的是:01 02 03 04 05 06 3 /// Str获取的是01 02 03 04 05 06 ...

  7. C#单纯的字母数字ASCII码转换

    字母转换成数字 byte[] array = new byte[1];   //定义一组数组array            array = System.Text.Encoding.ASCII.Ge ...

  8. C语言字符转换ASCII码

    //函 数 名:CharToHex()//功能描述:把ASCII字符转换为16进制//函数说明://调用函数://全局变量://输    入:ASCII字符//返    回:16进制///////// ...

  9. 关于ascii码的一些内容

    1.通过C#程序输出tab(制表符)内容. 1.1常用方式我们可以是 //测试输出\t到文件 File.WriteAllText("test.txt", "a\tb\tc ...

随机推荐

  1. 关于如何在本地IIS搭建网站

    步骤一: 首先安装与配置IIS服务,可借用百度经验:http://jingyan.baidu.com/article/d5a880eb75f74713f047cc57.html 步骤二: 修改web. ...

  2. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  3. Css 基础知识(一)

    1.Css概念 CSS 指层叠样式表 (Cascading Style Sheets)(级联样式表),Css是用来美化html标签的,相当于页面化妆. ◆样式表书写位置 2. 选择器 2.1.写法 选 ...

  4. List常用几种方式

    第一种,匹配俩个集合中相同的值 , , , , , , }; , , , , , , , , }; var C= listA.Intersect(listB); foreach (var item i ...

  5. 简单CNN 测试例

    1.训练数据: import tensorflow as tf import cv2 import os import numpy as np import time import matplotli ...

  6. Android--播放Gif的取巧办法

    由于做的项目,要有个动画的等待效果,第一时间想到的就是Gif(懒,省事),但是试了好多据说能播放Gif的控件,也写过,但是放到魅族手机上就是不能播放,所有就想了个招,既然Gif能在浏览器上播放,那an ...

  7. idea 关联 jdk

    1.打开IntelliJ IDEA 2.选择 "File" 菜单 3.找到 "other settings" 4.选择 “Structure for new P ...

  8. jsp 发布war 包到Tomcat

    1.将项目打包成war,打包过程这里不做赘述 2.在linux或者windows下安装xmapp 3.打开Tomcat下conf/server.xml,在host下添加一行        <Co ...

  9. 有关于《Linux C编程一站式学习》(备份)

    Linux C编程一站式学习 -- PDF版本,共37章: Linux C编程一站式学习 -- 在线版,来自灰狐: Linux C编程一站式学习 -- 在线版,来自亚嵌教育: Linux C一站式学习 ...

  10. [经典bug]弹框关闭按钮点击后程序闪退

    问题背景: 业务上遇到一个很诡异的问题:弹框界面上有一个关闭按钮,切换后台再返回后,点击关闭按钮,部分机型上会直接崩溃.点击手机返回键关闭界面则正常. 问题原因: 点击关闭按钮的操作属于UI线程,直接 ...