查看基本数据类型数据的存储小工具。

/**
*编程查看基本数据类型数据的存储小工具。
*用法:java BinaryCode -b/-h 查看的类型    查看的数据
*打包后的用法:java -jar -BinaryCode.jar -b/-h 查看类型 查看数据
*查看的类型:
*整数(byte short int long ):二进制补码
*浮点数(float double): IEEE754标准
*字符:(char):Java采用Unicode编码
*本地编码:计算机本地采用英文ASCII码,汉字内码
*/

public class BinaryCode 
{
    public static void main(String[] args) 
    {
        if((args.length != 3 ) || (!"-b".equals(args[0]) && !"-h".equals(args[0])))
        {
            System.out.println("用法:java BinaryCode -b/-h 查看的类型 查看的数据");
            System.out.println("说明:类型为byte、short、int、long、float、double、char、chars之一");
            System.exit(0);
        }

        if("-b".equals(args[0]))
        {
            System.out.println(args[2] + "(" + args[1] + ")的二进制编码为:");
        }
        else if("-h".equals(args[0]))
        {
            System.out.println(args[2] + "(" + args[1] + ")的十六进制编码为:");
        }

        //根据类型来执行对应的编码查看
        if("byte".equals(args[1]))
        {
            //查看byte型的二进制或者十六进制编码
            show(Byte.parseByte(args[2]), 1, args[0]);
        }

        else if("short".equals(args[1]))
        {
            //查看short型的二进制或者十六进制编码
            show(Short.parseShort(args[2]), 2, args[0]);
        }

        else if("int".equals(args[1]))
        {
            //查看int型的二进制或者十六进制编码
            show(Integer.parseInt(args[2]), 4, args[0]);
        }

        else if("long".equals(args[1]))
        {
            //查看long型的二进制或者十六进制编码
            show(Long.parseLong(args[2]), 8, args[0]);
        }

        else if("float".equals(args[1]))
        {
            //查看float型的二进制和十六进制编码
            show(Float.floatToIntBits(Float.parseFloat(args[2])), 4, args[0]);
        }

        else if("double".equals(args[1]))
        {
            //查看double型的二进制和十六进制编码
            show(Double.doubleToLongBits(Double.parseDouble(args[2])), 8, args[0]);
        }

        else if("char".equals(args[1]))
        {
            //查看字符的Unicode编码
            showChar(args[2],args[0]);
        }

        else if("chars".equals(args[1]))
        {
            //查看字符的本地编码,英文ASCII码,汉字内码
            showLocalChar(args[2].getBytes(),args[0]);
        
        }

        else
        {
            //类型不对,报错
            System.out.println("无此类型:" + args[1] + "!");
        }

        System.out.println();


    }

    /**
     *查看整数的二进制编码,占size字节大小
     *参数x:带查看的整数
     *参数size:该整数所占的字节数
     *参数mode:二进制或者十六进制展示
     */
    public static void show(long x, int size, String mode)
    {
        if("-b".equals(mode))        //如果是八进制
        {
            for(int i = size * 8 - 1; i>=0; i--)
            {
                System.out.print((x >>> i) & 1);
                if(i % 8 == 0)
                {
                    System.out.print(" ");
                
                }
            }
        
        }

        else if("-h".equals(mode))    //如果是十六进制
        {
            for(int i = size * 2 - 1; i>=0; i--)
            {
                System.out.printf("%X",(x >>> (i * 4)) & 0x0f);
                if(i % 2 == 0)
                {
                    System.out.print(" ");
                }
            }
        }
    }


    //查看字符串str的Unicode编码,mode为显示模式:二进制(-b)或者十六进制(-h)显示
    public static void showChar(String str, String mode)
    {
        for(int index = 0; index < str.length(); index++)
        {
            if("-b".equals(mode))
            {
                for(int i = 15; i>=0; i--)
                {
                    System.out.print((str.charAt(index) >>> i) & 1);
                    if(i % 8 == 0)
                    {
                        System.out.print(" ");
                    }
                }
            }

            else if("-h".equals(mode))
            {
                for(int i = 3; i>=0; i--)
                {
                    System.out.printf("%X", (str.charAt(index) >>> (i * 4)) & 0x0f);
                    if(i % 2 == 0)
                    {
                        System.out.print(" ");
                    }
                }
            }
        
        }
    }

    
    //查看字符串str的本地编码(英文ASCII码,汉字内码),mode为显示模式:二进制(-b)或者十六进制(-h)显示
    public static void showLocalChar(byte[] x, String mode)
    {
        for(int index = 0; index < x.length; index++)
        {
            if("-b".equals(mode))
            {
                for(int i = 7; i>=0; i--)
                {
                    System.out.print((x[index] >>> i) & 1);
                }
                System.out.print(" ");
            }

            else if("-h".equals(mode))
            {
                System.out.printf("%02X ", x[index] & 0x0f);
            }
        
        }
    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值