Assigning String Constants to Ubyte Arrays

General FreeBASIC programming questions.
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Assigning String Constants to Ubyte Arrays

Postby RockTheSchock » Apr 17, 2008 20:59

Code: Select all

Type foo
 signature(5) as UByte
 Declare Constructor()
End Type

Constructor foo
  this.signature(0) = asc("a")
  this.signature(1) = asc("a")
  this.signature(2) = asc("a")
  this.signature(3) = asc("a")
  this.signature(4) = asc("a")
  this.signature(5) = asc("a")
end Constructor


Does there exist a better way to assign the value. Replacing the Ubyte Array with a fixed length string wont't work because of the null char appended.


Code: Select all

Constructor foo
  this.signature = "aaaaaa" 'error here
end Constructor


Something like this would be nice.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Apr 17, 2008 21:43

Can you used fixed-length strings instead? Why is the null character a problem?

Code: Select all

Type foo
  As String * 6 signature
  Declare Constructor()
End Type

Constructor foo
  this.signature = "aabbcc"
End Constructor

Dim As foo shizzle

Print shizzle.signature
Print Chr(shizzle.signature[2])
Sleep


That's probably the best way to do it, but if you still want an array of ubytes, then you could use pointers.
I think you still need the extra byte, though. ;-)

Code: Select all

Type foo
  'As String * 6 signature
  As Ubyte signature(0 To 6)
  Declare Constructor()
End Type

Constructor foo
  'this.signature = "aabbcc"
  *cast(String Ptr, @this.signature(0)) = "aabbcc"
End Constructor

Dim As foo shizzle

Print *cast(String Ptr, @shizzle.signature(0))
Sleep
counting_pine
Site Admin
Posts: 6174
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Apr 17, 2008 21:43

You can do it using memcpy to copy the string data all at once, e.g.

Code: Select all

#include "crt/string.bi"

const SIG_SIZE = 6

type foo
 signature(0 to SIG_SIZE-1) as ubyte
 declare constructor()
 declare constructor( byref sig as string )
 declare property sig_str() as string
end type

constructor foo()
  memcpy( @signature(0), @"aaaaaa", SIG_SIZE )
end constructor

constructor foo( byref sig as string )
  memcpy( @signature(0), @sig[0], SIG_SIZE )
end constructor

property foo.sig_str() as string
 
  dim ret as string = space(SIG_SIZE)
  memcpy( @ret[0], @signature(0), SIG_SIZE )
 
  return ret
 
end property

dim as foo foo1 = "123456", foo2

print foo1.sig_str, foo2.sig_str
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » Apr 18, 2008 2:19

Actually, this should be the way to do it.. but for some reason, it doesn't work.

Code: Select all

type foo
    signature(5) as ubyte => {&H61,&H61,&H61,&H61,&H61,&H0}
    declare constructor()
end type

constructor foo
    print *cast(zstring ptr, @this.signature(0))   
end constructor
 
dim x as foo ' should print out "aaaaa"
dim signature(5) As ubyte => {&H61,&H61,&H61,&H61,&H61,&H0}
 
print *cast(zstring ptr, @x.signature(0))  ' still not working..
print *cast(zstring ptr, @signature(0))
 


Yet it works at module level, as expected.

Might be my now-outdated version of FB, I certainly hope it works for you, if it does not, that would mean that it's a bug that needs reporting.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Apr 18, 2008 3:31

Voodooattack, it works for me. ;-)
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Apr 18, 2008 4:32

Take this sample code into consideration too.

Code: Select all

#define FourCC( text )  { asc( text, 1 ), asc( text, 2 ), asc( text, 3 ), asc( text, 4 ) }

dim as ubyte   test( 0 to 3 ) = FourCC( "w007" )

for x as integer = 0 to 3
   print test( x ), chr( test( x ) )
next

Return to “General”

Who is online

Users browsing this forum: fxm and 3 guests