package com.neusoft.smart.pos.framework.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class StringUtils {

    /**
     * 将double格式化为指定小数位的String，不足小数位用0补全
     *
     * @param value     需要格式化的数字
     * @param scale 小数点后保留几位
     * @return
     */
    private static char[] HEX = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
    public static String roundByScale(BigDecimal value, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The   scale   must   be   a   positive   integer   or   zero");
        }
        if(scale == 0){
            return new DecimalFormat("0").format(value);
        }
        String formatStr = "0.";
        for(int i=0;i<scale;i++){
            formatStr = formatStr + "0";
        }
        return new DecimalFormat(formatStr).format(value);
    }
    /**
     * 将byte数组转换成16进制字符串
     *
     * @param data  需要转换的数组
     * @return
     */
    public static String bytesToHex(byte[] data) {
        if(null == data) return "";
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(HEX[b>>> 4 & 0xF]);
            sb.append(HEX[b & 0xF]);
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 将16进制字符串转换成byte数组
     *
     * @param data  需要转换的字符串
     * @return
     */
    public static byte[] hexToBytes(String data) {
        if(null == data || "".equals(data)) return null;
        int byteLen = data.length()%2==1? data.length()+1: data.length();
        byte[] ret =  new byte[byteLen /2];
        //TODO 用的时候再写
        return null;
    }
}
