Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Upending a Network value

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
dlh



Joined: 15 Dec 2006
Posts: 265
Location: ~Cincinnati

Posted: 27 January 2010, 19:14 PM    Post subject: Upending a Network value Reply with quote

I need to convert a big-endian network value to little-endian Unsigned Long. Does anyone have code for this?[/i]
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2493
Location: Portland, OR

Posted: 27 January 2010, 20:25 PM    Post subject: Re: Upending a Network value Reply with quote

dlh wrote:
Does anyone have code for this?
The code below can be used for swapping the byte order when changing from network order to host order and vice versa.
Code:
'
'' revByteOrder
'
' This function reverses the order of the bytes of a 32-bit value.
'
Function revByteOrder(ByVal val as UnsignedLong) as UnsignedLong
  ' define some based arrays for accessing the constituent bytes
  Dim valBytesIn() as Byte Based val.DataAddress
  Dim valBytesOut() as Byte Based revByteOrder.DataAddress

  ' reverse the byte order
  revByteOrder = CULng(valBytesIn(4))
  valBytesOut(2) = valBytesIn(3)
  valBytesOut(3) = valBytesIn(2)
  valBytesOut(4) = valBytesIn(1)
End Function
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2493
Location: Portland, OR

Posted: 27 January 2010, 20:38 PM    Post subject: Reply with quote

Here is a variation of the code above that is more uniform and demonstrates how to use a pragma to temporarily suppress a compiler warning. Without the pragma, the compiler will complain that not all paths through the function set the return value because the return value is not set directly. The warning includes the message number (2 in this case) and that is the number used for turning off the warning. The push and pop aspects of the pragma allow you to temporarily change the warning settings and then return to whatever was previously in effect.
Code:
#pragma warning(push; 2:Off)  ' turn off warning: return value not set
Function revByteOrder(ByVal val as UnsignedLong) as UnsignedLong
  ' define some based arrays for accessing the constituent bytes
  Dim valBytesIn() as Byte Based val.DataAddress
  Dim valBytesOut() as Byte Based revByteOrder.DataAddress

  ' reverse the byte order
  valBytesOut(1) = valBytesIn(4)
  valBytesOut(2) = valBytesIn(3)
  valBytesOut(3) = valBytesIn(2)
  valBytesOut(4) = valBytesIn(1)
End Function
#pragma warning(pop)
Back to top
dlh



Joined: 15 Dec 2006
Posts: 265
Location: ~Cincinnati

Posted: 27 January 2010, 23:47 PM    Post subject: Reply with quote

Thanks, Don.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group