/**
* BCD和ASCII转换工具类.
*
* @author xu.wei .
*/
public class BCDASCII {
/**
* 默认编码.
*/
private static final String ENCODING = "UTF-8";
/**
* A的ascii.
*/
public final static byte ALPHA_A_ASCII_VALUE = 0x41;
/**
* a的ascii.
*/
public final static byte ALPHA_a_ASCII_VALUE = 0x61;
/**
* 0的ascii.
*/
public final static byte DIGITAL_0_ASCII_VALUE = 0x30;
/**
* 构造函数 .
*/
private BCDASCII() {
}
/**
* BCD转成ASCII .
*
* @param bcdBuf
* bcd字节
* @param bcdOffset
* 位移
* @param asciiBuf
* ascii字节
* @param asciiOffset
* 位移
* @param asciiLen
* 长度
* @param rightAlignFlag
* 右对齐
*/
public static void fromBCDToASCII(byte[] bcdBuf, int bcdOffset,
byte[] asciiBuf, int asciiOffset, int asciiLen,
boolean rightAlignFlag) {
int cnt;
if (((asciiLen & 1) == 1) && rightAlignFlag) {
cnt = 1;
asciiLen++;
} else
cnt = 0;
for (; cnt < asciiLen; cnt++, asciiOffset++) {
asciiBuf[asciiOffset] = (byte) ((((cnt) & 1) == 1) ? (bcdBuf[bcdOffset++] & 0x0f)
: ((bcdBuf[bcdOffset] >> 4) & 0x0f));
asciiBuf[asciiOffset] = (byte) (asciiBuf[asciiOffset] + ((asciiBuf[asciiOffset] > 9) ? (ALPHA_A_ASCII_VALUE - 10)
: DIGITAL_0_ASCII_VALUE));
}
}
/**
* BCD转成ASCII .
*
* @param bcdBuf
* bcd字节
* @param bcdOffset
* 位移
* @param asciiLen
* ascii长度
* @param rightAlignFlag
* 右对齐
* @return ascii字节
*/
public static byte[] fromBCDToASCII(byte[] bcdBuf, int bcdOffset,
int asciiLen, boolean rightAlignFlag) {
byte[] asciiBuf = new byte[asciiLen];
fromBCDToASCII(bcdBuf, bcdOffset, asciiBuf, 0, asciiLen, rightAlignFlag);
return asciiBuf;
}
/**
* BCD转成ASCII .
*
* @param bcdBuf
* bcd字节
* @param bcdOffset
* 位移
* @param asciiLen
* ascii长度
* @param rightAlignFlag
* 右对齐
* @return ascii字符串
*/
public static String fromBCDToASCIIString(byte[] bcdBuf, int bcdOffset,
int asciiLen, boolean rightAlignFlag) {
try {
return new String(fromBCDToASCII(bcdBuf, bcdOffset, asciiLen,
rightAlignFlag), ENCODING);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* ASCII转成BCD .
*
* @param asciiBuf
* ascii字节
* @param asciiOffset
* ascii位移
* @param asciiLen
* ascii长度
* @param bcdBuf
* bcd字节
* @param bcdOffset
* bcd位移
* @param rightAlignFlag
* 是否右对齐
*/
public static void fromASCIIToBCD(byte[] asciiBuf, int asciiOffset,
int asciiLen, byte[] bcdBuf, int bcdOffset, boolean rightAlignFlag) {
int cnt;
byte ch, ch1;
if (((asciiLen & 1) == 1) && rightAlignFlag) {
ch1 = 0;
} else {
ch1 = 0x55;
}
for (cnt = 0; cnt < asciiLen; cnt++, asciiOffset++) {
if (asciiBuf[asciiOffset] >= ALPHA_a_ASCII_VALUE)
ch = (byte) (asciiBuf[asciiOffset] - ALPHA_a_ASCII_VALUE + 10);
else if (asciiBuf[asciiOffset] >= ALPHA_A_ASCII_VALUE)
ch = (byte) (asciiBuf[asciiOffset] - ALPHA_A_ASCII_VALUE + 10);
else if (asciiBuf[asciiOffset] >= DIGITAL_0_ASCII_VALUE)
ch = (byte) (asciiBuf[asciiOffset] - DIGITAL_0_ASCII_VALUE);
else
ch = 0x00;
if (ch1 == 0x55)
ch1 = ch;
else {
bcdBuf[bcdOffset] = (byte) (ch1 << 4 | ch);
bcdOffset++;
ch1 = 0x55;
}
}
if (ch1 != 0x55)
bcdBuf[bcdOffset] = (byte) (ch1 << 4);
}
/**
* ASCII转成BCD .
*
* @param asciiStr
* ascii字符串
* @param asciiOffset
* ascii位移
* @param asciiLen
* ascii长度
* @param bcdBuf
* bcd字节
* @param bcdOffset
* bcd位移
* @param rightAlignFlag
* 是否右对齐
*/
public static void fromASCIIToBCD(String asciiStr, int asciiOffset,
int asciiLen, byte[] bcdBuf, int bcdOffset, boolean rightAlignFlag) {
try {
byte[] asciiBuf = asciiStr.getBytes(ENCODING);
fromASCIIToBCD(asciiBuf, asciiOffset, asciiLen, bcdBuf, bcdOffset,
rightAlignFlag);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* ASCII转成BCD .
*
* @param asciiBuf
* ascii字节
* @param asciiOffset
* ascii位移
* @param asciiLen
* ascii长度
* @param rightAlignFlag
* 是否右对齐
* @return ascii字节
*/
public static byte[] fromASCIIToBCD(byte[] asciiBuf, int asciiOffset,
int asciiLen, boolean rightAlignFlag) {
byte[] bcdBuf = new byte[(asciiLen + 1) / 2];
fromASCIIToBCD(asciiBuf, asciiOffset, asciiLen, bcdBuf, 0,
rightAlignFlag);
return bcdBuf;
}
/**
* ASCII转成BCD .
*
* @param asciiStr
* ascii字符串
* @param asciiOffset
* ascii位移
* @param asciiLen
* ascii长度
* @param rightAlignFlag
* 是否右对齐
* @return ascii字符串
*/
public static byte[] fromASCIIToBCD(String asciiStr, int asciiOffset,
int asciiLen, boolean rightAlignFlag) {
try {
byte[] asciiBuf = asciiStr.getBytes(ENCODING);
return fromASCIIToBCD(asciiBuf, asciiOffset, asciiLen,
rightAlignFlag);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
ansix98算法java实现



AnsiX98算法java实现 ? PIN格式 ANSI X9.8 Format(带主帐号信息) PIN BLOCK 格式等于 PIN 按位异或主帐号: PIN 格式: BYTE 1 PIN的长度 BYTE 2 – BYTE 3/4/5/6/7 4--12个PIN(每个PIN占4个BIT) BYTE 4/5/6/7/8 – BYTE 8 FILLER “F” (每个“F“占4个BIT) 主帐号格式: BYTE 1 — BYTE 2 0X0000 BYTE 3 — BYTE 8 12个主帐号 12位主帐号的取法:取主帐号的右12位(不包括最右边的校验位),不足12位左补“0X00”。 例如:明文 PIN 123456, 设:磁卡上的主帐号为:123456789012345678 截取下的主帐号为:678901234567 则 用于PIN加密的主帐号为:0x00 0x00 0x67 0x89 0x01 0x23 0x45 0x67 则 PIN BLOCK 为 0x06 0x12 0x34 0x56 0xFF 0xFF 0xFF 0xFF 异或 0x00 0x00 0x67 0x89 0x01 0x23 0x45 0x67 结果为 0x06 0x12 0x53 0xDF 0xFE 0xDC 0xBA 0x98 设:磁卡上的主帐号为:1234567890123456 截取下的主帐号为:456789012345 则用于PIN加密的主帐号为:0x00 0x00 0x45 0x67 0x89 0x01 0x23 0x45 则 PIN BLOCK 为 0x06 0x12 0x34 0x56 0xFF 0xFF 0xFF 0xFF 异或 0x00 0x00 0x45 0x67 0x89 0x01 0x23 0x45 结果为 0x06 0x12 0x71 0x31 0x76 0xFE 0xDC 0xBA

























- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- Missএ᭄大表锅2016-08-01很好用,不错哦。
- xiaohj22042014-03-15很好,省去很多麻烦
- xg061gxh2016-03-10很好,很好,整缺这个算法呢

- 粉丝: 3
- 资源: 3
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 精品推荐-2024 ChatGPT大模型技术场景与商业应用视频精讲合集(45课).zip
- 苏苏源码-springboot416-农产品电子商务网站(编号:34074366).zip
- 直流电机双闭环调速系统仿真与详细设计报告:转速稳定,灵活调节,适应多变负载与电网电压环境,直流电机双闭环调速系统仿真与详细设计报告:转速稳定,灵活调节,适应多变负载与电网电压环境,直流电机双闭环(电流
- Java Web开发中使用HttpServletRequestWrapper实现可重复读取请求的技术方案
- 英飞凌电源反极性保护电路分析
- 基于强化学习算法Q-learning的水库优化调度研究-探索Python代码实现与优化策略,强化学习驱动的水库优化调度策略研究:基于Q-learning算法的智能决策与实施路径,python代码-基
- 近邻传播聚类算法(AP算法)的MATLAB实现:无需预设聚类数目与中心,便捷高准确度的操作方法 ,近邻传播聚类算法(AP算法)的Matlab实现:无需预设聚类数目与中心,便捷操作,高准确度,新颖方法探
- 基于MATLAB与CPLEX求解器的电转气协同碳捕集虚拟电厂优化调度策略研究,基于MATLAB与CPLEX求解器的电转气协同碳捕集虚拟电厂优化调度研究,MATLAB代码:计及电转气协同的含碳捕集与垃圾
- PSO算法优化SVM模型在时间序列预测分析中的应用:代码详解与数据替换指南,详细注释的SVM时间序列预测分析:PSO优化算法,高效通用代码模板,直接替换数据即运行,PSO优化SVM做时间序列预测分析
- COMSOL激光打孔与水平集两相流仿真模型探究:温度场与流场一体化分析的两个版本介绍,COMSOL激光打孔与水平集两相流仿真模型:温度场流场深度解析,双版本探究,comsol激光打孔(不通)水平集两相
- 异步电机故障仿真研究:定子绕组匝间短路与转子断条故障的模拟分析,异步电机定子绕组匝间短路与转子断条故障仿真分析:探究感应电机匝间短路故障的机理与影响,异步电机定子绕组匝间短路仿真,转子断条故障仿真,感
- 日前日内两阶段调度综合能源分析:基于Matlab与Yalmip的程序优化结果对比及成本评估,日前日内两阶段调度综合能源分析:基于Matlab与Yalmip的程序优化结果对比及机组成本与弃风惩罚的探讨
- 基于PLC的智能农业温室大棚控制系统的电气控制方案 包括梯形图程序、接线图及原理图设计、IO分配与组态画面展示,基于PLC的智能农业温室大棚控制:电气控制组态、梯形图程序与画面组态详解,基于PLC的智
- 机器人研究数模实验方案:基于LQR控制的仿真优化与测试,适用于机械臂、无人机等镇定与轨迹追踪控制算法,通过Simulink平台快速搭建仿真平台,机器人研究Simulink平台自动化搭建与测试方案:LQ
- 基于正向开发的P2混合动力轿车并联模型:先算整车阻力再精准分配扭矩,P2轿车并联模型:正向开发下的整车阻力计算与扭矩分配策略,P2轿车并联模型,完全基于正向开发,先计算整车阻力,然后根据当前车辆模式进
- 混合储能系统能量管理Simulink仿真模型:蓄电池与超级电容协同控制策略及SOC限值管理,混合储能系统能量管理Simulink仿真模型研究:蓄电池与超级电容的功率分配与控制策略,混合储能系统能量管理


