-3

Bytes of recorded audio was sent together with HTTP request body. When the request is received on the server side, the audio data looks like this:

\u0000\u0000\u0000\u0000\u0000\u0000��\u0002\u0000������4\u0000M\u0000@\u0000%\u0000\u0014\u0000����������\u0015\u0000M\u0000r\u0000�\u0000_\u0000C\u0000^\u0000V\u0000\u0007\u0000��\"\u0000;\u0000>\u0000\u0005\u0000����������������\f\u0000+\u0000K\u0000e\u0000.\u0000������������\u0003\u0000\b\u0000����������\"\u0000G\u0000V\u0000(\u0000 \u0000\u0004\u0000����������\u0003\u0000W\u0000�\u0000Z\u0000a\u0000{\u0000,\u0000��������\u001E\u0000��������1\u0000\u001A\u0000\u0011\u0000(\u0000/\u0000\u0016\u0000��������0\u0000/\u00002\u0000;\u0000������d�������\u001F\u00009\u00006\u0000j\u0000[\u0000'\u0000������������\u000E\u00009\u0000%\u0000����\u0015\u0000(\u00003\u0000+\u0000'\u0000������<\u0000F\u0000=\u0000h\u0000�\u0000M\u0000��������������T\u0000i\u0000]

How can I convert this string back to the original audio data bytes?

I tried calling getBytes() but it doesn't seem right.

----- EDIT -----

Posting the code for HTTP Request would be too long. I'll cite a short sample here.

public static void main(String args[]) throws Exception
{
    byte[] test = new byte[]{(byte)0xfc, (byte)0xff, (byte)0xff, (byte)0xff};

    String testString = new String(test);

    System.out.println(testString);

    System.out.println(getHexString(testString.getBytes()));
}

public static String getHexString(byte[] bytes) 
{
    char[] hexArray = "0123456789ABCDEF".toCharArray();
    char[] hexChars = new char[bytes.length * 2];

    for ( int j = 0; j < bytes.length; j++ ) 
    {
        int v = bytes[j] & 0xFF;

        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }

    return new String(hexChars);
}

The code above gives the following result

����
EFBFBDEFBFBDEFBFBDEFBFBD

When I convert bytes FC, FF, FF, FF to string, I get ����. When I convert the string back to bytes I get EF BF BD EF BF BD EF BF BD EF BF BD. I want to get the original bytes FC, FF, FF, FF from ����.

8
  • 4
    That very depends on details such as encoding, file formats, and most importantly: how exactly your http request is built from. Please provide a minimal reproducible example, right now your question is on a level of "hello doctor, my cat is making strange noises. now tell me whats wrong with her". Commented Jul 13, 2018 at 10:05
  • 1
    If you can post the code that was used to produce the HTTP request on the client side, perhaps someone can help. Commented Jul 13, 2018 at 10:19
  • @GhostCat Hi, I just edited my question. Commented Jul 13, 2018 at 10:37
  • @user31601 Hi, I just edited my question Commented Jul 13, 2018 at 10:37
  • byte is a signed type. 0xFF is not a valid byte! Commented Jul 13, 2018 at 10:51

1 Answer 1

0

Strings are meant to represent text. Audio data is not text. Try as you may, it is not possible to shove some audio data in a String, then try to extract back your audio data out of the String. It is designed to not work.

As the audio data you want to work with are not text, then you have no rationale to want to involve String in the process. Forget about Strings, you now know that they are not a suitable tool for your goal, so leave them alone completely. Binary data such as audio, can be represented as sequences of bytes. Such as a byte[] array for instance.

Remove the variable String testString. Don't build it, don't declare it, remove it entirely. It's a String, and you can't use Strings for your goal, so get rid of it.

Instead, let's focus on using your variable byte[] test as it is. It's already a byte[] and it contains all the data. So it's all you need.

You can write it with System.out.write(test); but in the console that's not very useful. In a socket's OutputStream that's perfect though.

Or you can write out the hexadecimal encoded version of it with:

System.out.println(getHexString(test));

Here you obtain a fully functional result. Decoding them back to bytes will give you exactly the bytes you started with.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree, you captured the essential problem with strings here. But then: we really dont know anything about how those audio data is coming in on his http request. In other words: you addressed those parts of the question that can in fact be answered. Anything else is unclear...
@GhostCat Of course. Still, it's important enough to be said and insisted on: Strings are the wrong tool to work on non-text binary. If you're working with non-text binary and you're putting it in a String at some point, then you've failed and your program won't work. Since it's the inevitable consequence, then, don't use String for anything else but text.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.