+ Reply to Thread
Results 1 to 4 of 4

Converting a string to a utf8 byte array

Hybrid View

  1. #1
    Registered User
    Join Date
    04-23-2011
    Location
    Guildford
    MS-Off Ver
    Excel 2007
    Posts
    21

    Converting a string to a utf8 byte array

    Temp = QByteArray(TX_Data.toUtf8());
    (Function in C i need to replicate)

    I need to convert from an ascii string into a utf-8 encoded byte array to satisfy the encoding required by data packets being sent out from an excel spreadsheet to the comm port.

    Have tried looking around but can t really find anything suitable - best I found was the code for vba but not compatible with vba - please help


    Public Function ConvertStringToUtf8Bytes(ByRef strText As String) As Byte()
    
        Dim objStream As ADODB.Stream
        Dim data() As Byte
        
        ' init stream
        Set objStream = New ADODB.Stream
        objStream.Charset = "utf-8"
        objStream.Mode = adModeReadWrite
        objStream.Type = adTypeText
        objStream.Open
        
        ' write bytes into stream
        objStream.WriteText strText
        objStream.Flush
        
        ' rewind stream and read text
        objStream.Position = 0
        objStream.Type = adTypeBinary
        objStream.Read 3 ' skip first 3 bytes as this is the utf-8 marker
        data = objStream.Read()
        
        ' close up and return
        objStream.Close
        ConvertStringToUtf8Bytes = data
    
    End Function

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Converting a string to a utf8 byte array

    Does this work?

    Function ToUTF8(sInp As String) As Byte()
        ToUTF8 = StrConv(sInp, vbFromUnicode)
    End Function
    
    Sub Test()
        Dim ab() As Byte
        
        ab = ToUTF8("abcd")
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Converting a string to a utf8 byte array

    If it needs a null terminator ...
    Function ToUTF8(sInp As String) As Byte()
        Dim ab() As Byte
    
        ab = StrConv(sInp, vbFromUnicode)
        ReDim Preserve ab(UBound(ab) + 1)
        ToUTF8 = ab
    End Function

  4. #4
    Registered User
    Join Date
    04-23-2011
    Location
    Guildford
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Converting a string to a utf8 byte array

    not in the lab till monday will give it a go then

    thanks
    Last edited by shg; 05-14-2011 at 12:39 PM. Reason: deleted spurious quote

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1