How to read value of type "IBuffer" as string in c#?
Asked Answered
M

2

9

How can I copy a variable which is of type IBuffer in C# (UWP app) to a string? The IBuffer itself doesn't seem to have any methods. It has Length which seems to be the correct value. But I cannot see the value in debugger (says requires Native debugging). Below is the class. I need to get Data.

public sealed class MagneticStripeReaderTrackData : IMagneticStripeReaderTrackData
{
    public IBuffer Data { get; }
Mealymouthed answered 31/5, 2016 at 22:50 Comment(2)
IBuffer sounds like an interface - you'll need an implementation to see any data.Whisker
Yeah IBuffer interface is from namespace Windows.Storage.StreamsMealymouthed
A
20

For example you can use it like this:

var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(Data);
var output = dataReader.ReadString(Data.Length);

You can find same example here. https://msdn.microsoft.com/ru-ru/library/windows/apps/hh464978

Are answered 31/5, 2016 at 23:0 Comment(1)
Thank you I've needed this for a IoT applicationHenbane
L
0

You can convert your IBuffer stream to a DataReader using the FromBuffer method and then using the ReadString method of the DataReader you can retrieve your data as a string.

    public string RetrieveStringFromUtf8IBuffer(Windows.Storage.Streams.IBuffer theBuffer)
    {
        using (var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(theBuffer))
        {
            dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
            return dataReader.ReadString(theBuffer.Length);
        }
    }
Laic answered 8/9, 2021 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.