2014-11-06 129 views
1

有沒有將字符串的值轉換爲Java中的long值的方法? 顯然,String.format("%040x", new BigInteger(1, message.getBytes(message)));只改變它的格式而不是值。我需要更改該值,因爲我將使用轉換變量進行加密(PRINCE)過程,該過程將以長(十六進制)格式進行處理。將變量值從字符串轉換爲Java中的long

public void onClick(View v, String args) 
       {    
       String phoneNo = txtPhoneNo.getText().toString(); 
       String message = txtMessage.getText().toString();    
        if (phoneNo.length()>0 && message.length()>0){ 
         //prince pri = new prince(); 
         //message = toLong(); 
         String.format("%040x", new BigInteger(1, message.getBytes(message))); 
         prince.Encrypt (message, k0, kop, k1, t); 
         sendSMS(phoneNo, message); }    
        else 
        Toast.makeText(getBaseContext(), 
          "Please enter both phone number and message.", 
          Toast.LENGTH_SHORT).show(); 
       } 
+0

舉一個'message'內容的例子。 – 2014-11-06 12:49:32

+0

它可以是任何支持160個字符的句子,因爲這個應用程序用於短信@Aaron Digulla – Anon 2014-11-06 12:52:07

+0

你不覺得在這種情況下你需要更多的「長」值嗎?如何描述PRINCE所需的輸入值,然後詢問如何將某種類型的消息轉換爲輸入值? – 2014-11-06 13:07:05

回答

0

您可以使用Long.parseLong()

new BigInteger(1, message.getBytes(message))應該不起作用。首先,message.getBytes()會爲未知的字符集引發異常。

此外,new BigInteger(1, message.getBytes("UTF-8"))將返回大多數輸入的奇數結果。

我的猜測是你的問題確實是:我如何將一個字符串轉換爲字節並將其轉換爲十六進制字符串?

解決方案:

byte[] data = message.getBytes("UTF-8"); 

要獲得從字符串的字節數組。然後這個問題告訴你如何將它轉換爲一個十六進制字符串:How to convert a byte array to a hex string in Java?

0

可能有更多的問題,然後人們可能會想,所以我創建了一個小代碼示例,使用特定的編碼(拉丁文-1 ,它接近於SMS接受的內容),然後返回LongBuffer,其中包含編碼爲long的值,使用PKCS#5/7填充。

import java.nio.ByteBuffer; 
import java.nio.LongBuffer; 
import java.nio.charset.StandardCharsets; 

public class TooLong { 

    private static int PRINCE_BLOCK_SIZE = 64/Byte.SIZE; 

    /** 
    * Converts a message to a LongBuffer that is completely filled. 
    * Latin encoding and PKCS#7 padding are applied to the message. 
    * @param message the message 
    * @return the message as converted 
    */ 
    private static LongBuffer messageToLongBuffer(String message) { 
     byte[] messageData = message.getBytes(StandardCharsets.ISO_8859_1); 
     int paddingSize = PRINCE_BLOCK_SIZE - messageData.length % PRINCE_BLOCK_SIZE; 
     ByteBuffer messageBuf = ByteBuffer.allocate(messageData.length + paddingSize); 
     messageBuf.put(messageData); 
     for (int i = 0; i < paddingSize; i++) { 
      messageBuf.put((byte) paddingSize); 
     } 
     messageBuf.flip(); 
     return messageBuf.asLongBuffer(); 
    } 

    public static void main(String[] args) { 
     String message = "owlstead gets your problem"; 
     LongBuffer messageBuf = messageToLongBuffer(message); 
     // the long buffer will be completely filled, so you can 
     // use longBuffer.array() as well if you prefer long[] instead 
     while (messageBuf.hasRemaining()) { 
      System.out.printf("%016X ", messageBuf.get()); 
     } 

     // in case you need it as long[] (flip only needed because of the printf statement before) 
     messageBuf.flip(); 
     long[] messageData = new long[messageBuf.remaining()]; 
     messageBuf.get(messageData); 
    } 
} 
+0

這可以使用CharsetEncoder進行優化,當然也可以更智能地處理緩衝區,但對於SMS,這應該很好。 – 2014-11-06 13:42:26

+0

請注意,您必須對您的密文進行編碼以符合[SMS字符集](http://stackoverflow.com/questions/5186702/looking-for-a-list-of-valid-characters-that-c​​an-be -sent功能於短信文本消息) – 2014-11-06 13:43:20