ubyte array to string

New to FreeBASIC? Post your questions here.
1134i
Posts: 62
Joined: Jun 05, 2005 4:39
Location: Vladivostok, Russia
Contact:

ubyte array to string

Postby 1134i » May 11, 2006 6:18

Simple way to convert UBYTE array to STRING is memcpy?

or i can do:

ubyte = string? or something else?
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » May 11, 2006 6:40

You can use cptr, or cast if you just need to cast the type, otherwise i think memcpy is the way to do it.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » May 11, 2006 7:32

1. Do you have an array of ubyte or a pointer to ubyte?

2. What direction do you want to copy? Your question asks for an array of ubyte to string, but your pseudo-code looks like you want to copy a string into an array of ubyte.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » May 11, 2006 7:58

Code: Select all

Function Cva ( f As uByte Ptr, l As Integer, start As Integer = 0 ) As String

  Return fb_StrAllocTempDescF( f[start], l )
   
End Function



Dim As uByte r( 6 ) => { Asc( "H" ), Asc( "e" ), Asc( "l" ), Asc( "l" ), Asc( "o" ) }


Dim As String s

s = cva( Varptr( r( 0 ) ), 6 )


? s



Sleep






redcrab
Posts: 619
Joined: Feb 07, 2006 15:29
Location: France / Luxemburg
Contact:

Postby redcrab » May 11, 2006 8:19

cha0s wrote:

Code: Select all

Function Cva ( f As uByte Ptr, l As Integer, start As Integer = 0 ) As String
  Return fb_StrAllocTempDescF( f[start], l )
End Function
Dim As uByte r( 6 ) => { Asc( "H" ), Asc( "e" ), Asc( "l" ), Asc( "l" ), Asc( "o" ) }
Dim As String s
s = cva( Varptr( r( 0 ) ), 6 )
? s
Sleep



Awesome ! Didn't known that FB can do it with its lib.

That's fun !
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » May 11, 2006 8:31

mmm... nice work there cha0s!!

i think that would allow for runtime data creation, having something like this sounds awesome:

Code: Select all

   
    Dim mystr As String PTR
    Dim addr  As Any PTR
   
   
    addr = Allocate(30)
   
    mystr = New String(addr,4) '(address, initial size)

    *mystr = "1234" 'ok

    *mystr = "hello!" 'the rtlib reallocates automatically..
   
    print *mystr
   
    sleep
   
    end
   


yum!!! would be really flexible imho.. :D~
1134i
Posts: 62
Joined: Jun 05, 2005 4:39
Location: Vladivostok, Russia
Contact:

Postby 1134i » May 11, 2006 9:01

cha0s wrote:

Code: Select all

Function Cva ( f As uByte Ptr, l As Integer, start As Integer = 0 ) As String

  Return fb_StrAllocTempDescF( f[start], l )
   
End Function



Dim As uByte r( 6 ) => { Asc( "H" ), Asc( "e" ), Asc( "l" ), Asc( "l" ), Asc( "o" ) }


Dim As String s

s = cva( Varptr( r( 0 ) ), 6 )


? s



Sleep



Thanks!
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » May 11, 2006 18:31

old tricks ;)

mjs originally posted that one btw
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Postby Antoni » May 11, 2006 19:50

coderJeff
Site Admin
Posts: 3135
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Postby coderJeff » May 11, 2006 22:16

If the ubyte array happens to be null terminated, and you don't actually need nulls in the string, can also use a cast:

Code: Select all

dim b(0 to 5) as ubyte = _
  { 72,69,76,76,79,0 }

dim x as string
x = *cast(zstring ptr, @b(0))
? x

Depends on what data is in the UByte array and what needs to be done with it.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest