org.apache.kylin.common.util.Bytes.toBinaryFromHex()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.3k)|赞(0)|评价(0)|浏览(91)

本文整理了Java中org.apache.kylin.common.util.Bytes.toBinaryFromHex()方法的一些代码示例,展示了Bytes.toBinaryFromHex()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bytes.toBinaryFromHex()方法的具体详情如下:
包路径:org.apache.kylin.common.util.Bytes
类名称:Bytes
方法名:toBinaryFromHex

Bytes.toBinaryFromHex介绍

[英]Takes a ASCII digit in the range A-F0-9 and returns the corresponding integer/ordinal value.
[中]获取范围为a-F0-9的ASCII数字,并返回相应的整数值/序数值。

代码示例

代码示例来源:origin: apache/kylin

/**
 * this method only works for hex strings
 */
public static byte[] fromReadableText(String text) {
  String[] tokens = text.split("\\\\x");
  byte[] ret = new byte[tokens.length - 1];
  for (int i = 1; i < tokens.length; ++i) {
    int x = Bytes.toBinaryFromHex((byte) tokens[i].charAt(0));
    x = x << 4;
    int y = Bytes.toBinaryFromHex((byte) tokens[i].charAt(1));
    ret[i - 1] = (byte) (x + y);
  }
  return ret;
}

代码示例来源:origin: apache/kylin

/**
 * Create a byte array from a string of hash digits. The length of the
 * string must be a multiple of 2
 *
 * @param hex
 */
public static byte[] fromHex(String hex) {
  checkArgument(hex.length() > 0, LENGTH_MUST_BE_GREATER_THAN_0);
  checkArgument(hex.length() % 2 == 0, "length must be a multiple of 2");
  // Make sure letters are upper case
  hex = hex.toUpperCase(Locale.ROOT);
  byte[] b = new byte[hex.length() / 2];
  for (int i = 0; i < b.length; i++) {
    b[i] = (byte) ((toBinaryFromHex((byte) hex.charAt(2 * i)) << 4)
        + (toBinaryFromHex((byte) hex.charAt((2 * i + 1))) & 0xff));
  }
  return b;
}

代码示例来源:origin: apache/kylin

public static byte[] toBytesBinary(String in) {
  // this may be bigger than we need, but let's be safe.
  byte[] b = new byte[in.length()];
  int size = 0;
  for (int i = 0; i < in.length(); ++i) {
    char ch = in.charAt(i);
    if (ch == '\\' && in.length() > i + 1 && in.charAt(i + 1) == 'x') {
      // ok, take next 2 hex digits.
      char hd1 = in.charAt(i + 2);
      char hd2 = in.charAt(i + 3);
      // they need to be A-F0-9:
      if (!isHexDigit(hd1) || !isHexDigit(hd2)) {
        // bogus escape code, ignore:
        continue;
      }
      // turn hex ASCII digit -> number
      byte d = (byte) ((toBinaryFromHex((byte) hd1) << 4) + (toBinaryFromHex((byte) hd2)) & 0xff);
      b[size++] = d;
      i += 3; // skip 3
    } else {
      b[size++] = (byte) ch;
    }
  }
  // resize:
  byte[] b2 = new byte[size];
  System.arraycopy(b, 0, b2, 0, size);
  return b2;
}

代码示例来源:origin: org.apache.kylin/kylin-core-common

/**
 * this method only works for hex strings
 */
public static byte[] fromReadableText(String text) {
  String[] tokens = text.split("\\\\x");
  byte[] ret = new byte[tokens.length - 1];
  for (int i = 1; i < tokens.length; ++i) {
    int x = Bytes.toBinaryFromHex((byte) tokens[i].charAt(0));
    x = x << 4;
    int y = Bytes.toBinaryFromHex((byte) tokens[i].charAt(1));
    ret[i - 1] = (byte) (x + y);
  }
  return ret;
}

代码示例来源:origin: org.apache.kylin/kylin-common

/**
 * this method only works for hex strings
 */
public static byte[] fromReadableText(String text) {
  String[] tokens = text.split("\\\\x");
  byte[] ret = new byte[tokens.length - 1];
  for (int i = 1; i < tokens.length; ++i) {
    int x = Bytes.toBinaryFromHex((byte) tokens[i].charAt(0));
    x = x << 4;
    int y = Bytes.toBinaryFromHex((byte) tokens[i].charAt(1));
    ret[i - 1] = (byte) (x + y);
  }
  return ret;
}

代码示例来源:origin: org.apache.kylin/kylin-common

/**
 * Create a byte array from a string of hash digits. The length of the
 * string must be a multiple of 2
 *
 * @param hex
 */
public static byte[] fromHex(String hex) {
  checkArgument(hex.length() > 0, "length must be greater than 0");
  checkArgument(hex.length() % 2 == 0, "length must be a multiple of 2");
  // Make sure letters are upper case
  hex = hex.toUpperCase();
  byte[] b = new byte[hex.length() / 2];
  for (int i = 0; i < b.length; i++) {
    b[i] = (byte) ((toBinaryFromHex((byte) hex.charAt(2 * i)) << 4) + toBinaryFromHex((byte) hex.charAt((2 * i + 1))));
  }
  return b;
}

代码示例来源:origin: org.apache.kylin/kylin-core-common

/**
 * Create a byte array from a string of hash digits. The length of the
 * string must be a multiple of 2
 *
 * @param hex
 */
public static byte[] fromHex(String hex) {
  checkArgument(hex.length() > 0, LENGTH_MUST_BE_GREATER_THAN_0);
  checkArgument(hex.length() % 2 == 0, "length must be a multiple of 2");
  // Make sure letters are upper case
  hex = hex.toUpperCase(Locale.ROOT);
  byte[] b = new byte[hex.length() / 2];
  for (int i = 0; i < b.length; i++) {
    b[i] = (byte) ((toBinaryFromHex((byte) hex.charAt(2 * i)) << 4)
        + (toBinaryFromHex((byte) hex.charAt((2 * i + 1))) & 0xff));
  }
  return b;
}

代码示例来源:origin: org.apache.kylin/kylin-core-common

public static byte[] toBytesBinary(String in) {
  // this may be bigger than we need, but let's be safe.
  byte[] b = new byte[in.length()];
  int size = 0;
  for (int i = 0; i < in.length(); ++i) {
    char ch = in.charAt(i);
    if (ch == '\\' && in.length() > i + 1 && in.charAt(i + 1) == 'x') {
      // ok, take next 2 hex digits.
      char hd1 = in.charAt(i + 2);
      char hd2 = in.charAt(i + 3);
      // they need to be A-F0-9:
      if (!isHexDigit(hd1) || !isHexDigit(hd2)) {
        // bogus escape code, ignore:
        continue;
      }
      // turn hex ASCII digit -> number
      byte d = (byte) ((toBinaryFromHex((byte) hd1) << 4) + (toBinaryFromHex((byte) hd2)) & 0xff);
      b[size++] = d;
      i += 3; // skip 3
    } else {
      b[size++] = (byte) ch;
    }
  }
  // resize:
  byte[] b2 = new byte[size];
  System.arraycopy(b, 0, b2, 0, size);
  return b2;
}

代码示例来源:origin: org.apache.kylin/kylin-common

public static byte[] toBytesBinary(String in) {
  // this may be bigger than we need, but let's be safe.
  byte[] b = new byte[in.length()];
  int size = 0;
  for (int i = 0; i < in.length(); ++i) {
    char ch = in.charAt(i);
    if (ch == '\\' && in.length() > i + 1 && in.charAt(i + 1) == 'x') {
      // ok, take next 2 hex digits.
      char hd1 = in.charAt(i + 2);
      char hd2 = in.charAt(i + 3);
      // they need to be A-F0-9:
      if (!isHexDigit(hd1) || !isHexDigit(hd2)) {
        // bogus escape code, ignore:
        continue;
      }
      // turn hex ASCII digit -> number
      byte d = (byte) ((toBinaryFromHex((byte) hd1) << 4) + toBinaryFromHex((byte) hd2));
      b[size++] = d;
      i += 3; // skip 3
    } else {
      b[size++] = (byte) ch;
    }
  }
  // resize:
  byte[] b2 = new byte[size];
  System.arraycopy(b, 0, b2, 0, size);
  return b2;
}

相关文章