`
收藏列表
标题 标签 来源
Java 与 C 底层数据类型转换 Java 与 C 底层数据类型转换
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * 仅仅适用于 Java 与 C++ 通讯中,网络流解析与生成使用
 *
 * 高低位互换(Big-Endian 大头在前 & Little-Endian 小头在前)。
 * 举例而言,有一个4字节的数据0x01020304,要存储在内存中或文件中编号0˜3字节的位置,两种字节序的排列方式分别如下:
 * <pre>
 * Big Endian
 *  
 * 低地址                           高地址
 * ----------------------------------------------------&gt;
 * 地址编号
 * |     0      |      1     |     2       |      3    |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |     01     |      02    |     03      |     04    |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * 
 * Little Endian
 * 
 * 低地址                           高地址
 * ----------------------------------------------------&gt;
 * 地址编号
 * |     0      |      1     |     2       |      3    |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |     04     |      03    |     02      |     01    |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * </pre>
 * Java则统一使用big模式
 * c中的unsigned short  对应着java中的char两个字节,无符号
 * c的无符号int,short,byte字节数组,相应转换成java的long,char,short
 *
 * @author Snowolf
 * @version 1.0
 * @since 1.0
 */
public abstract class CIOUtil {
	public static final String CHARSET = "UTF-8";

	/**
	 * 从输入流中读布尔 
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static boolean readBoolean(DataInputStream is) throws IOException {
		return is.readBoolean();
	}

	/**
	 * 从流中读定长度字节数组
	 * 
	 * @param is
	 * @param s
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBytes(DataInputStream is, int i)
			throws IOException {
		byte[] data = new byte[i];
		is.readFully(data);

		return data;
	}

	/**
	 * 从输入流中读字符 
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static char readChar(DataInputStream is) throws IOException {
		return (char) readShort(is);
	}

	/**
	 * 从输入流中读双精度 
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static double readDouble(DataInputStream is) throws IOException {
		return Double.longBitsToDouble(readLong(is));
	}

	/**
	 * 从输入流中读单精度
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static float readFloat(DataInputStream is) throws IOException {
		return Float.intBitsToFloat(readInt(is));
	}

	/**
	 * 从流中读整型
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static int readInt(DataInputStream is) throws IOException {
		return Integer.reverseBytes(is.readInt());
	}

	/**
	 * 从流中读长整型
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static long readLong(DataInputStream is) throws IOException {
		return Long.reverseBytes(is.readLong());
	}

	/**
	 * 从流中读短整型
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static short readShort(DataInputStream is) throws IOException {
		return Short.reverseBytes(is.readShort());
	}

	/**
	 * 从输入流中读字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static String readUTF(DataInputStream is) throws IOException {
		short s = readShort(is);
		byte[] str = new byte[s];

		is.readFully(str);

		return new String(str, CHARSET);
	}

	/**
	 * 向输出流中写布尔
	 * 
	 * @param os
	 * @param b
	 * @throws IOException
	 */
	public static void writeBoolean(DataOutputStream os, boolean b)
			throws IOException {
		os.writeBoolean(b);
	}

	/**
	 * 向输出流中写字节数组
	 * 
	 * @param os
	 * @param data
	 * @throws IOException
	 */
	public static void writeBytes(DataOutputStream os, byte[] data)
			throws IOException {
		os.write(data);
	}

	/**
	 * 向输出流中写字符
	 * 
	 * @param os
	 * @param b
	 * @throws IOException
	 */
	public static void writeChar(DataOutputStream os, char b)
			throws IOException {
		writeShort(os, (short) b);
	}

	/**
	 * 向输出流中写双精度
	 * 
	 * @param os
	 * @param d
	 * @throws IOException
	 */
	public static void writeDouble(DataOutputStream os, double d)
			throws IOException {
		writeLong(os, Double.doubleToLongBits(d));
	}

	/**
	 * 向输出流中写单精度
	 * 
	 * @param os
	 * @param f
	 * @throws IOException
	 */
	public static void writeFloat(DataOutputStream os, float f)
			throws IOException {
		writeInt(os, Float.floatToIntBits(f));
	}

	/**
	 * 向输出流中写整型
	 * 
	 * @param os
	 * @param i
	 * @throws IOException
	 */
	public static void writeInt(DataOutputStream os, int i) throws IOException {
		os.writeInt(Integer.reverseBytes(i));
	}

	/**
	 * 向输出流中写长整型
	 * 
	 * @param os
	 * @param l
	 * @throws IOException
	 */
	public static void writeLong(DataOutputStream os, long l)
			throws IOException {
		os.writeLong(Long.reverseBytes(l));
	}

	/**
	 * 向输出流中写短整型
	 * 
	 * @param os
	 * @param s
	 * @throws IOException
	 */
	public static void writeShort(DataOutputStream os, short s)
			throws IOException {
		os.writeShort(Short.reverseBytes(s));
	}

	/**
	 * 向输出流中写字符串 字符串 结构 为 一个指定字符串字节长度的短整型+实际字符串
	 * 
	 * @param os
	 * @param str
	 * @throws IOException
	 */
	public static void writeUTF(DataOutputStream os, String str)
			throws IOException {
		byte[] data = str.getBytes(CHARSET);
		writeShort(os, (short) data.length);
		os.write(data);
	}

}

人民币转成大写 java工具 http://www.javaeye.com/topic/941019
/**
 * 人民币转成大写
 *
 * @param value
 * @return String
 */
public static String hangeToBig(double value)
{
    char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示
    char[] vunit = { '万', '亿' }; // 段名表示
    char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示
    long midVal = (long) (value * 100); // 转化成整形
    String valStr = String.valueOf(midVal); // 转化成字符串

    String head = valStr.substring(0, valStr.length() - 2); // 取整数部分
    String rail = valStr.substring(valStr.length() - 2); // 取小数部分

    String prefix = ""; // 整数部分转化的结果
    String suffix = ""; // 小数部分转化的结果
    // 处理小数点后面的数
    if (rail.equals("00"))
    { // 如果小数部分为0
        suffix = "整";
    }
    else
    {
        suffix = digit[rail.charAt(0) - '0'] + "角" + digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来
    }
    // 处理小数点前面的数
    char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组
    char zero = '0'; // 标志'0'表示出现过0
    byte zeroSerNum = 0; // 连续出现0的次数
    for (int i = 0; i < chDig.length; i++)
    { // 循环处理每个数字
        int idx = (chDig.length - i - 1) % 4; // 取段内位置
        int vidx = (chDig.length - i - 1) / 4; // 取段位置
        if (chDig[i] == '0')
        { // 如果当前字符是0
            zeroSerNum++; // 连续0次数递增
            if (zero == '0')
            { // 标志
                zero = digit[0];
            }
            else if (idx == 0 && vidx > 0 && zeroSerNum < 4)
            {
                prefix += vunit[vidx - 1];
                zero = '0';
            }
            continue;
        }
        zeroSerNum = 0; // 连续0次数清零
        if (zero != '0')
        { // 如果标志不为0,则加上,例如万,亿什么的
            prefix += zero;
            zero = '0';
        }
        prefix += digit[chDig[i] - '0']; // 转化该数字表示
        if (idx > 0)
            prefix += hunit[idx - 1];
        if (idx == 0 && vidx > 0)
        {
            prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿
        }
    }

    if (prefix.length() > 0)
        prefix += '圆'; // 如果整数部分存在,则有圆的字样
    return prefix + suffix; // 返回正确表示
} 
Global site tag (gtag.js) - Google Analytics