Here you can find the source of stringToLongUnknownLength(String str, int startIndex)
public static long stringToLongUnknownLength(String str, int startIndex)
//package com.java2s; /*/* ww w .ja v a 2 s .c o m*/ * Copyright (c) 2016 Red Hat, Inc., Rob Stryker, and Contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main { public static long stringToLongUnknownLength(String str, int startIndex) { for (int i = startIndex; i < str.length(); i++) { char c = str.charAt(i); switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'x': break; default: return stringToLong(str.substring(startIndex, i)); } } return 0; } public static long stringToLong(String offsetString) { if (offsetString.charAt(0) == '0') { if (offsetString.length() > 1 && (offsetString.charAt(1) == 'x' || offsetString.charAt(1) == 'X')) { String ss = offsetString.substring(2); try { return Long.parseLong(ss, 16); } catch (NumberFormatException nfe) { // It's probably out of bounds, if it's a quad, so let's // try hex2bytes and get a long from that byte[] bytes = hex2Bytes(ss); ByteBuffer bb = ByteBuffer.allocate(8).wrap(bytes).order(ByteOrder.BIG_ENDIAN); long l = bb.getLong(); return l; } } return Long.parseLong(offsetString, 8); } return Long.parseLong(offsetString); } public static byte[] hex2Bytes(String s) { int len = s.length(); byte[] data = new byte[len / 2]; for (int i = 0; i < len; i += 2) { data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)); } return data; } }