2

您将如何将字符串存储/表示为长字符串?然后把它塞进一个 8 字节的数组中?

我尝试过/使用过的东西

    String eidString = "Awesome!";
    ByteBuffer buf = ByteBuffer.allocate(8);
    CharBuffer cbuf = buf.asCharBuffer();
    cbuf.put(eidString);

    byte[] eid = ByteBuffer.allocate(8).putLong(cbuf ??);

尝试 2

    Long l = Long.valueOf("Awesome!");

    byte[] eid = ByteBuffer.allocate(8).putLong(l).array();

    long p = ByteBuffer.wrap(eid).getLong();

    System.out.println(p);

尝试 3

String input = "hello long world";

byte[] bytes = input.getBytes();
LongBuffer tmpBuf = ByteBuffer.wrap(bytes).asLongBuffer();

long[] lArr = new long[tmpBuf.remaining()];
for (int i = 0; i < lArr.length; i++)
    lArr[i] = tmpBuf.get();

System.out.println(input);
System.out.println(Arrays.toString(lArr));
// store longs...

// ...load longs
long[] longs = { 7522537965568945263L, 7955362964116237412L };
byte[] inputBytes = new byte[longs.length * 8];
ByteBuffer bbuf = ByteBuffer.wrap(inputBytes);
for (long l : longs)
    bbuf.putLong(l);
System.out.println(new String(inputBytes));
4

4 回答 4

3

您需要将字符串编码为数字并将其反转。

  • 你必须确定你需要的符号数量。例如,64 个符号需要 6 位。32 个符号需要 5 位。
  • 这将确定字符串的最大长度。例如,对于 6 位 => 64/6 = 10 个符号,对于 8 位 => 64/8 = 8 个符号。例如,“hello long world”将不适合,除非您假设并非所有 az 都可用。

完成此操作后,您可以像解析 10 或 36 基数一样对符号进行编码。要变回字符串,您可以执行相反的操作(例如打印以 10 或 36 为基数的数字)

可能的字符/符号的范围是多少?(您需要包含一个终止符号,因为字符串的长度可能会有所不同)

于 2013-01-03T19:49:20.343 回答
2

如果您接受以下有限字符集:

 a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
 A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,
 0,1,2,3,4,5,6,7,8,9, , <-- space character

那么您的简化字母表中将有 63 个符号R。每个符号都可以映射到 6 位表示(64 位组合的唯一组合)。有一个隐含的第 64 个符号,它是用于标记字符串终止的空符号,应该用 表示0x00。这给我们留下了一个R映射到 6 位的字母表作为双射。

Along有 64 位信息。因为64/6 = 10,这意味着我们可以R在 Javalong变量中存储一个长度最多为 10 个字符的字符串。

值得注意的是,即使R是简化的字母表并且我们的字符串长度限制为 10,我们仍然可以表达有意义的英语短语,将它们转换为 a long,然后再返回!

Java 代码(64 位映射):

public static long stringToLong(String s) {
  if(s.length() >10) { throw new IllegalArgumentException("String is too long: "+s); }
  long out = 0L;
  for(int i=0; i<s.length(); ++i) {
    long m = reducedMapping(s.codePointAt(i));
    if (m==-1) { throw new IllegalArgumentException("Unmapped Character in String: "+s); }
    m <<= ((9-i)*6)+4;
    out |= m;
  }
  return out;
}

public static String longToString(long l) {
  String out = "";
  long m = 0xFC00000000000000L;
  for(int i=0; i<10; ++i,m>>>=6) {
    int x =(int)( (l&m) >>> (((9-i)*6)+4));
    if(x==0) { break; }
    out += mapping[x];
  }
  return out;
}


public static long reducedMapping(int x) {
  long out=-1;
       if(x >= 97 && x <= 122) { out = (long)(x-96); } //  'a' =>  1 : 0x01
  else if(x >= 65 && x <=  90) { out = (long)(x-37); } //  'A' => 27 : 0x1B
  else if(x >= 48 && x <=  57) { out = (long)(x-+5); } //  '0' => 53 : 0x35
  else if(x == 32 )            { out = 63L;          } //  ' ' => 63 : 0x3F
  return out;
}
public static char[] mapping = {
'\n', //<-- unused/empty character
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9',' '
};

Java 代码(32 位映射):

public static long stringToLong32(String s) {
      if(s.length() >12) { throw new IllegalArgumentException("String is too long: "+s); }
      long out = 0L;
      for(int i=0; i<s.length(); ++i) {
        long m = reducedMapping32(s.codePointAt(i));
        if (m==-1) { throw new IllegalArgumentException("Unmapped Character in String: "+s); }
        m <<= ((11-i)*5)+4;
        out |= m;
      }
      return out;
    }

public static String longToString32(long l) {
      String out = "";
      long m = 0xF800000000000000L;
      for(int i=0; i<12; ++i,m>>>=5) {
        int x =(int)( (l&m) >>> (((11-i)*5)+4));
        if(x==0) { break; }
        out += mapping32[x];
      }
      return out;
    }

public static long reducedMapping32(int x) {
      long out=-1;
           if(x >= 97 && x <= 122) { out = (long)(x-96); } //  'a' =>  1 : 0x01
      else if(x >= 65 && x <=  90) { out = (long)(x-64); } //  'A' =>  1 : 0x01
      else if(x >= 32 && x <= 34)  { out = (long)(x-5);  } //  ' ','!','"' => 27,28,29
      else if(x == 44 )            { out = 30L;          } //  ',' => 30 : 0x1E
      else if(x == 46 )            { out = 31L;          } //  '.' => 31 : 0x1F
      return out;
    }
public static char[] mapping32 = {
    '\n', //<-- unused/empty character
    'a','b','c','d','e','f','g','h','i','j','k','l','m',
    'n','o','p','q','r','s','t','u','v','w','x','y','z',
    ' ','!','"',',','.' 
    };
}


编辑:

使用此类进行更通用的转换。它允许Strings任何非空字符集唯一地映射到 along并再次转换回原始字符String。只需通过构造函数定义任何字符集 ( ),然后使用生成的对象使用&char[]来回转换。str2longlong2str

JavaStringAndLongConverter类:

import java.util.Arrays;
import java.util.regex.Pattern;

public class StringAndLongConveter {

  /* .,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,., */
  /*   String <--> long ID Conversion   */
  /* `'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`' */

  /* --<[ Class Members ]>-- */
  // Don't re-arrange, order-dependent initializations

  private final char[]  CHAR_MAP;
  public  final int     NUM_ACTUAL_CHARS;
  public  final int     NUM_MAPPED_CHARS;
  public  final int     BIT_COUNT;
  public  final int     MAX_NUM_CHARS;
  public  final long    ROLLING_MASK;
  public  final long    FORMAT_MASK;
  public  final long    MIN_VALUE;
  public  final long    MAX_VALUE;
  public  final Pattern REGEX_CHAR_VALIDATOR;

  public StringAndLongConveter(char[] chars) {
    if(chars == null  ) { throw new IllegalArgumentException("Cannot Pass in null reference"); }
    if(chars.length==0) { throw new IllegalArgumentException("Cannot Pass in empty set"     ); }
    CHAR_MAP             = setCharMap(chars);
    NUM_ACTUAL_CHARS     = CHAR_MAP.length;
    NUM_MAPPED_CHARS     = NUM_ACTUAL_CHARS+1;
    BIT_COUNT            = calcMinBitsNeeded();
    MAX_NUM_CHARS        = calcMaxPossibleChars();
    ROLLING_MASK         = calcRollingMask();
    FORMAT_MASK          = calcFormatMask();
    MIN_VALUE            = calcIDMinVal();
    MAX_VALUE            = calcIDMaxVal();
    REGEX_CHAR_VALIDATOR = createRegExValidator();
  }

  /* --<[ Dynamic Initialization Calculation Helper Methods ]>-- */

  //Remove duplicates
  private final char[] setCharMap(final char[] chars) {
    char[] tmp = new char[chars.length];    
    int dupes = 0;
    for(int i=0; i<chars.length; ++i) {
      boolean dupeFound = false;
      for(int j=0; !dupeFound && j<i; ++j) {
        if(chars[i]==chars[j]) {
          ++dupes;
          dupeFound = true;   
        }
      }
      if(!dupeFound) { tmp[i-dupes] = chars[i]; }
    }
    char[] out = new char[chars.length-dupes];
    if(dupes==0) { out = chars; }
    else {
      for(int i=0; i<out.length; ++i) out[i] = tmp[i];
    }
    return out;
  } 
  // calculate minimum bits necessary to encode characters uniquely
  private final int calcMinBitsNeeded() {
    if(NUM_MAPPED_CHARS==0) { return 0; }
    int val,tmp,log;
    val = NUM_MAPPED_CHARS;
    tmp = Integer.highestOneBit(val); // returns only the highest set bit
    tmp = tmp | (tmp-1);              // propagate left bits
    log = Integer.bitCount(tmp);      // count bits (logarithm base 2)
    return ((val&(val-1))==0) ? log-1 : log;
    //return one less then bit count if even power of two
  }
  //Calculate maximum number of characters that can be encoded in long
  private final int calcMaxPossibleChars() {
    return Long.SIZE/BIT_COUNT;
  }
  //Calculate rolling mask for str <--> long conversion loops
  private final long calcRollingMask() {
    long   mask = 0x0000000000000001L;
    for(int i=1; i<BIT_COUNT;     ++i) { mask  |= mask << 1; }
    for(int i=1; i<MAX_NUM_CHARS; ++i) { mask <<= BIT_COUNT; }
    return mask;
  }
  //Calculate format mask for long input format validation
  private final long calcFormatMask() {
    //propagate lest significant set bit in rolling mask & negate resulting value
    return ~(ROLLING_MASK | (ROLLING_MASK-1));
  }
  //Calculate min value of long encoding
  //doubles as format specification for unused bits
  private final long calcIDMinVal() {
    return 0xAAAAAAAAAAAAAAAAL & FORMAT_MASK;
  }
  //Calculate max value of long encoding
  private final long calcIDMaxVal(){
    char   maxChar    = CHAR_MAP[CHAR_MAP.length-1];
    char[] maxCharArr = new char[MAX_NUM_CHARS];
    Arrays.fill(maxCharArr, maxChar);
    return str2long(new String(maxCharArr));
  }

  //Dynamically create RegEx validation string for invalid characters
  private final Pattern createRegExValidator() {
    return Pattern.compile("^["+Pattern.quote(new String(CHAR_MAP))+"]+?$");
  }

  /* --<[ Internal Helper Methods ]>-- */

  private static boolean ulongLessThen(long lh, long rh) {
    return (((lh ^ rh) >> 63) == 0) ? lh < rh : (0x8000000000000000L & lh)==0;
  }

  private long charMapping(final char c) {
    for(int i=0; i<CHAR_MAP.length; ++i)
      if(CHAR_MAP[i]==c)
        return i+1;
    return -1;
  }

  /* --<[ String <--> long Conversion Methods ]>-- */

  public  final String long2str(final long n) {
    String out = "";
    if (ulongLessThen(n,MIN_VALUE) || ulongLessThen(MAX_VALUE,n)) { throw new IllegalArgumentException("Long Outside of Formatted Range: "+Long.toHexString(n)); }
    if ((FORMAT_MASK & n) != MIN_VALUE)                           { throw new IllegalArgumentException("Improperly Formatted long"); }
    long m = ROLLING_MASK;
    for(int i=0; i<MAX_NUM_CHARS; ++i,m>>>=BIT_COUNT) {
      int x =(int)( (n&m) >>> ((MAX_NUM_CHARS-i-1)*BIT_COUNT));//10|10 0111
      if(x >= NUM_MAPPED_CHARS) { throw new IllegalArgumentException("Invalid Formatted bit mapping: \nlong="+Long.toHexString(n)+"\n masked="+Long.toHexString(n&m)+"\n i="+i+" x="+x); }
      if(x==0) { break; }
      out += CHAR_MAP[x-1];
    }
    return out;
  }

  public  final long str2long(String str) {
    if(str.length() > MAX_NUM_CHARS) { throw new IllegalArgumentException("String is too long: "+str); }
    long out = MIN_VALUE;
    for(int i=0; i<str.length(); ++i) {
      long m = charMapping(str.charAt(i));
      if (m==-1) { throw new IllegalArgumentException("Unmapped Character in String: "+str); }
      m <<= ((MAX_NUM_CHARS-i-1)*BIT_COUNT);
      out += m; // += is more destructive then |= allowing errors to be more readily detected 
    }
    return out;
  }

  public  final boolean isValidString(String str) {
    return str != null && !str.equals("")               //null or empty String
       &&  str.length() <= MAX_NUM_CHARS                //too long
       &&  REGEX_CHAR_VALIDATOR.matcher(str).matches(); //only valid chars in string
  }

  public final char[] getMappedChars() { return Arrays.copyOf(CHAR_MAP,CHAR_MAP.length); }

}

玩得开心Stringlong解码

于 2013-01-26T21:32:08.123 回答
1

要将 String 解析为 Long,可以使用 Long 包装类。

String myString = "1500000";
Long myLong = Long.parseLong(myString);

要将其填充到一个 8 字节数组中...

long value = myLong.longValue();
byte[] bytes = new byte[8];
for (int i = 0; i < bytes.length; i++) {
   long mask = 0xFF00000000000000 >> (i * 8);
   bytes[i] = (byte) (value & mask);   
}

这个例子是大端。

如果要将字符串编码为长字符串,则可以执行以下操作:

String myString = "HELLO";
long toLong = 0;
for (int i = 0; i < myString.length(); i++) {
   long c = (long) myString.charAt(i);
   int shift = (myString.length() - 1 - i) * 8;
   toLong += c << shift;
}

这还没有经过测试。它可能有一些问题。

于 2013-01-03T18:43:22.003 回答
0

你不需要这样做。String 有一个名为 getBytes() 的方法。它直接为您进行转换。使用参数“哈利路亚”调用以下方法

public static void strToLong(String s) throws IOException
{
    byte[] bArr = s.getBytes();
    for( byte b : bArr)
    {
        System.out.print(" " + b);
    }
    System.out.println();

    System.out.write(bArr);
}

结果是

 72 97 108 108 101 108 117 106 97 104
Hallelujah
于 2013-01-03T19:58:08.973 回答