locked
Web API. How can I return 32 base data and an object? RRS feed

  • Question

  • User-40926958 posted

    I develop web service to mobile clients with Web API.

    I aim that return byte data, which is .wav voice data, and an object.

    Byte data will be 64 base in object after return to Web API consumer, Because Web API returns data with JSON object.

    I want to return data as 32base.

    How can i do it?

    Example:

    I can return just byte data as 32 base. audioByteWaw is a byte[] data But this example doesnt returns an extra object with bytes

                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content =   new ByteArrayContent(audioByteWav),
                };

    Also, I can do this sample code. resultResponse is an object which includes primitive properties.. string,int, bool. This example converts byte data to 64 base.

    ControllerContext.Request.CreateResponse(HttpStatusCode.OK, new{ ByteArrayContent = new ByteArrayContent(audioByteWav) ,ResultResponse = resultResponse});

    How can i return 32base byte[] data and an extra object together to Web API consumer?

    Friday, November 11, 2016 12:33 PM

All replies

  • User-2057865890 posted

    Hi Oguzhankarsli2,

    If you want to return Base32, there are some packages which implements Base32 encoder and decoder.

    https://www.nuget.org/packages?q=base32 

    ZBase32Encoder

    public static class ZBase32Encoder
        {
            private const string EncodingTable = "ybndrfg8ejkmcpqxot1uwisza345h769";
    
            private static readonly byte[] DecodingTable = new byte[128];
    
            static ZBase32Encoder()
            {
                for (var i = 0; i < DecodingTable.Length; ++i)
                {
                    DecodingTable[i] = byte.MaxValue;
                }
    
                for (var i = 0; i < EncodingTable.Length; ++i)
                {
                    DecodingTable[EncodingTable[i]] = (byte)i;
                }
            }
    
            public static string Encode(byte[] data)
            {
                if (data == null)
                {
                    throw new ArgumentNullException("data");
                }
    
                var encodedResult = new StringBuilder((int)Math.Ceiling(data.Length * 8.0 / 5.0));
    
                for (var i = 0; i < data.Length; i += 5)
                {
                    var byteCount = Math.Min(5, data.Length - i);
    
                    ulong buffer = 0;
                    for (var j = 0; j < byteCount; ++j)
                    {
                        buffer = (buffer << 8) | data[i + j];
                    }
    
                    var bitCount = byteCount * 8;
                    while (bitCount > 0)
                    {
                        var index = bitCount >= 5
                                    ? (int)(buffer >> (bitCount - 5)) & 0x1f
                                    : (int)(buffer & (ulong)(0x1f >> (5 - bitCount))) << (5 - bitCount);
    
                        encodedResult.Append(EncodingTable[index]);
                        bitCount -= 5;
                    }
                }
    
                return encodedResult.ToString();
            }
    
            public static byte[] Decode(string data)
            {
                if (data == string.Empty)
                {
                    return new byte[0];
                }
    
                var result = new List<byte>((int)Math.Ceiling(data.Length * 5.0 / 8.0));
    
                var index = new int[8];
                for (var i = 0; i < data.Length;)
                {
                    i = CreateIndexByOctetAndMovePosition(ref data, i, ref index);
    
                    var shortByteCount = 0;
                    ulong buffer = 0;
                    for (var j = 0; j < 8 && index[j] != -1; ++j)
                    {
                        buffer = (buffer << 5) | (ulong)(DecodingTable[index[j]] & 0x1f);
                        shortByteCount++;
                    }
    
                    var bitCount = shortByteCount * 5;
                    while (bitCount >= 8)
                    {
                        result.Add((byte)((buffer >> (bitCount - 8)) & 0xff));
                        bitCount -= 8;
                    }
                }
    
                return result.ToArray();
            }
    
            private static int CreateIndexByOctetAndMovePosition(ref string data, int currentPosition, ref int[] index)
            {
                var j = 0;
                while (j < 8)
                {
                    if (currentPosition >= data.Length)
                    {
                        index[j++] = -1;
                        continue;
                    }
    
                    if (IgnoredSymbol(data[currentPosition]))
                    {
                        currentPosition++;
                        continue;
                    }
    
                    index[j] = data[currentPosition];
                    j++;
                    currentPosition++;
                }
    
                return currentPosition;
            }
    
            private static bool IgnoredSymbol(char checkedSymbol)
            {
                return checkedSymbol >= DecodingTable.Length || DecodingTable[checkedSymbol] == byte.MaxValue;
            }
        }
    }

    Best Regards,

    Chris

    Tuesday, November 15, 2016 6:37 AM