Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Finding a sequence of bytes in a ByteArray

I need to find a sequence of bytes within a ByteArray, but there doesn't seem to be any direct operations to do so. What's the most direct route to determining if a particular sequence of bytes exists within a ByteArray?

For example, I'd like to capture the range of a sequence (or null) with something like this:

val searchArray = arrayOf(0xF1.toByte(), 0xF2.toByte(), 0xF4.toByte(), 0xF8.toByte())
val range = myByteArray.range(of: searchArray)

I'd also be happy with just the starting index, since I always know the size of my search array.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

its too simple to add guava just for it:

fun ByteArray.findFirst(sequence: ByteArray,startFrom: Int = 0): Int {
    if(sequence.isEmpty()) throw IllegalArgumentException("non-empty byte sequence is required")
    if(startFrom < 0 ) throw IllegalArgumentException("startFrom must be non-negative")
    var matchOffset = 0
    var start = startFrom
    var offset = startFrom
    while( offset < size ) {
        if( this[offset] == sequence[matchOffset]) {
            if( matchOffset++ == 0 ) start = offset
            if( matchOffset == sequence.size ) return start
        }
        else
            matchOffset = 0
        offset++
    }
    return -1
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda