怎么样才能在java里也把汉字转换成DBCS码,例如 把'李'转换成编码C0EE 
谢谢!急。

解决方案 »

  1.   

    没有人知道呀?唉,早知道这样,当时在VB里就用AscW就好了,用unicode应没问题。
      

  2.   

    char c = '李';
    String s = String.valueOf(c);
    byte[] bytes = s.getBytes();
    for(int i=0; i<bytes.length; i++)
        System.out.print(Integer.toHexString(bytes[i] & 0xff));
      

  3.   

    楼上的,你真厉害!果然是对的。我马上给分。
    什么原理呀?编码unicode和DBCS对汉字来说有什么不同呀?
    我还想请教你一个问题,不知道你对VB熟不熟?
    请帮我看看,过会补分:
    因为原来网络客户端加密英文字符串时用的如下程序:
    '加密
    Private Function Cipher(ByVal password As String, ByVal from_text As String) As String
        Const MIN_ASC = 32  ' Space.
        Const MAX_ASC = 126 ' ~.
        Const NUM_ASC = MAX_ASC - MIN_ASC + 1
        Dim Offset As Long
        Dim str_len As Integer
        Dim i As Integer
        Dim ch As Integer
        Dim to_text
        Offset = NumericPassword(password)
        Rnd -1
        Randomize Offset
        str_len = Len(from_text)
        For i = 1 To str_len
            ch = Asc(Mid$(from_text, i, 1))
            If ch >= MIN_ASC And ch <= MAX_ASC Then
                ch = ch - MIN_ASC
                Offset = Int((NUM_ASC + 1) * Rnd)
                ch = ((ch + Offset) Mod NUM_ASC)
                ch = ch + MIN_ASC
                to_text = to_text & Chr$(ch)
            End If
        Next i
        Cipher = to_text
    End Function
    '解密
    Private Function Decipher(ByVal password As String, ByVal from_text As String) As String
        Const MIN_ASC = 32  ' Space.
        Const MAX_ASC = 126 ' ~.
        Const NUM_ASC = MAX_ASC - MIN_ASC + 1
        Dim Offset As Long
        Dim str_len As Integer
        Dim i As Integer
        Dim ch As Integer
        Dim to_text
        Offset = NumericPassword(password)
        Rnd -1
        Randomize Offset
        str_len = Len(from_text)
        For i = 1 To str_len
            ch = Asc(Mid$(from_text, i, 1))
            If ch >= MIN_ASC And ch <= MAX_ASC Then
                ch = ch - MIN_ASC
                Offset = Int((NUM_ASC + 1) * Rnd)
                ch = ((ch - Offset) Mod NUM_ASC)
                If ch < 0 Then ch = ch + NUM_ASC
                ch = ch + MIN_ASC
                to_text = to_text & Chr$(ch)
            End If
        Next i
        Decipher = to_text
    End Function
    Private Function NumericPassword(ByVal password As String) As Long
    Dim value As Long
    Dim ch As Long
    Dim shift1 As Long
    Dim shift2 As Long
    Dim i As Integer
    Dim str_len As Integer
        str_len = Len(password)
        For i = 1 To str_len
            ' Add the next letter.
            ch = Asc(Mid$(password, i, 1))
            value = value Xor (ch * 2 ^ shift1)
            value = value Xor (ch * 2 ^ shift2)
            shift1 = (shift1 + 7) Mod 19
            shift2 = (shift2 + 13) Mod 23
        Next i
        NumericPassword = value
    End Function我在服务端想用java解密。但是就是因为:Rnd 这个值(生成的随机序列和java里生成的不一样) 所以没办法了!关键就是要用java写时也和上面的VB写的生成的随机数序列一样就好了。我想真的可能是没有办法一样的。因为一个是VB的,一个是java的随机生成器。
    想问问,有没有办法?谢啦!!!
    ^_^
      

  4.   

    1. 找一下VB中的rnd用的什么算法,然后在Java里实现
    2. 把加解密的代码做成一个DLL,在Java里用JNI调用
      

  5.   

    byte[] bytes = s.getBytes(); //返回系统字符集编码方式的字节数组,中文Windows下为GBK等价于
    byte[] bytes = s.getBytes("GBK");
      

  6.   

    谢谢cbhyk !!
    1. VB里的rnd算法没地方得知,源码又不公开,我只会用它。Rnd 函数  
    返回一个包含随机数值的 Single。
    语法
    Rnd[(number)]
    可选的 number 参数是 Single 或任何有效的数值表达式。
    返回值
    如果 number 的值是 Rnd 生成 
    小于 0 每次都使用 number 作为随机数种子得到的相同结果。 
    大于 0 序列中的下一个随机数。 
    等于 0 最近生成的数。 
    省略 序列中的下一个随机数。 
    说明
    Rnd 函数返回小于 1 但大于或等于 0 的值。
    number 的值决定了 Rnd 生成随机数的方式。
    对最初给定的种子都会生成相同的数列,因为每一次调用 Rnd 函数都用数列中的前一个数作为下一个数的种子。
    在调用 Rnd 之前,先使用无参数的 Randomize 语句初始化随机数生成器,该生成器具有根据系统计时器得到的种子。
    注意 若想得到重复的随机数序列,在使用具有数值参数的 Randomize 之前直接调用具有负参数值的 Rnd。使用具有同样 number 值的 Randomize 是不会得到重复的随机数序列的。Randomize 语句
    初始化随机数生成器。
    语法
    Randomize [number]
    可选的 number 参数是 Variant 或任何有效的数值表达式。
    说明
    Randomize 用 number 将 Rnd 函数的随机数生成器初始化,该随机数生成器给 number 一个新的种子值。如果省略 number,则用系统计时器返回的值作为新的种子值。
    如果没有使用 Randomize,则(无参数的)Rnd 函数使用第一次调用 Rnd 函数的种子值。
    注意 若想得到重复的随机数序列,在使用具有数值参数的 Randomize 之前直接调用具有负参数值的 Rnd。使用具有同样 number 值的 Randomize 是不会得到重复的随机数序列的。
    2.做成DLL,我用VB能把它做成ActiveX DLL,在java里用JNI调用,只能在Windows平台上吧?
    我这个java程序是要运行在linux上的。谢谢指点。 :-)
      

  7.   

    那,怎么把字符串"C0EE"还原为字符'李'呀?搞定了就结贴。
      

  8.   

    byte[] data = new byte[2];
    data[0] = (byte) 0xC0;
    data[1] = (byte) 0xEE;String s = new String(data);
    char c = s.charAt(0);
    System.out.println(c);