B4R Question Please introduce a function for convert ASCII to Char

BertI

Member
Licensed User
On the other hand:
B4X:
    Dim c As Byte = 65
    Log(c)
gives 65 rather than A, so what's the neatest way to convert c to be seen as a character in that context? I must admit I have encountered such situations myself at times and have dealt with them by just doing things a different way to begin with, but I don't know that the results look too neat.
 
Upvote 0

BertI

Member
Licensed User
Yes that does do the job, thanks, but I guess it highlights the problems I have trying to understand when strings are interchangeable with byte arrays. I had in fact tried the below, since my thoughts were that such a char function would be expected to return a string type:
B4X:
private Sub char(b As Byte) As String
    Dim a As String = Array As Byte(b)
    Return a
End Sub
Of course the IDE throws up a warning #7 at line 2 about object being converted to a string as being perhaps a programming mistake, but if a string is a byte array then why so? The result from this was not 65 nor A but 1073741648
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
If you cast a byte array to 'Strings', the last byte is set to 0, so in your sub the string (as byte array) has two bytes and the log shows the internal id of the array rather then its contents.

All of this is because of the memory limitations of microprocessors, not because of B4R.

The Log function renders the Byte array of one Byte as a String of one Character.
 
Last edited:
Upvote 0

BertI

Member
Licensed User
If you cast a byte array to 'Strings', the last byte is set to 0, so in your sub the string (as byte array) has two bytes and the log shows the internal id of the array rather then its contents.

All of this is because of the memory limitations of microprocessors, not because of B4R.

The Log function renders the Byte array of one Byte as a String of one Character.
I do understand that a string is fundamentally held as an array (or you might even say string :)) of bytes with the last byte having a value 0, but in my example if the string 'a' is just rendered into an array of bytes (my interpretation of the word 'cast') then it would be as if 'a' is just the same as array of bytes(65,0). However this doesn't appear to be exactly the case since:
B4X:
    Dim e() As Byte = Array As Byte(65,0)
    Log(e)
returns an 'A', whereas my sub doesn't. So it must mean that at least the log function treats the return from my sub differently than just an array of bytes. I have indeed read the tutorial on Strings and Bytes in the past but still not quite understanding why the above subtleties are so. Anyhow it's just another one of those curiosities of mine
 
Upvote 0
Top