做数据格式转换的DatatypeConverter类的例子程序

转自:http://www.concretepage.com/java/example_datatypeconverter_java

参考:Class DatatypeConverter


If we need to convert XML schema data types, then we need to through knowledge of xml schema data type and lexical representation. DatatypeConverter class of the package javax.xml.bind can do all to convert xml schema data type to java data type. DatatypeConverter has print and parse methods. print method encodes the data into lexical representation of xsd. And parse method can decode the lexical representation to string.

【yasi】printXXX 的函数就是encode,parseXXX 的函数就是decode。比如,String printBase64Binary(byte[])就是将字节数组做base64编码,byte[] parseBase64Binary(String) 就是将Base64编码后的String还原成字节数组。

注意,下面代码第10行,传给printBase64Binary 的参数是 s1.getBytes(),而不是 s1 本身。

DatatypeConverterTest.java

    Find the sample example.
    package com.concretepage;
    import java.util.Calendar;
    import javax.xml.bind.DatatypeConverter;
    public class DatatypeConverterTest {
    public static void main(String[] args) {
    //Testing DatatypeConverter.printBase64Binary
    String s1 = "Testing DatatypeConverter.printBase64Binary";
    String encodeds1 = DatatypeConverter.printBase64Binary(s1.getBytes());
    System.out.println(encodeds1);
    byte[] decodeds1= DatatypeConverter.parseBase64Binary(encodeds1);
    System.out.println(new String(decodeds1));
    //Testing DatatypeConverter.printHexBinary
    String s2 = "Testing DatatypeConverter.printHexBinary";
    String encodeds2 = DatatypeConverter.printHexBinary(s2.getBytes());
    System.out.println(encodeds2);
    byte[] decodeds2= DatatypeConverter.parseHexBinary(encodeds2);
    System.out.println(new String(decodeds2));
    //Lexical representation of date time
    System.out.println(DatatypeConverter.printDateTime(Calendar.getInstance()));
    }
    }

Output

VGVzdGluZyBEYXRhdHlwZUNvbnZlcnRlci5wcmludEJhc2U2NEJpbmFyeQ==
Testing DatatypeConverter.printBase64Binary
54657374696E6720204461746174797065436F6E7665727465722E7072696E7448657842696E617279
Testing DatatypeConverter.printHexBinary
2013-03-15T18:23:25.830+05:30


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值