Data conversion

  • Thread starter Thread starter Peter Royle
  • Start date Start date
P

Peter Royle

Hello,

I need to get a 2-byte number into a string, but I DON'T want VB to
obligingly turn it into a string - in other words, I want to end up
with 2 bytes in the string, which is the number as held in the int16
variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))

but this is giving me problems unstringing it again, on a Pocket PC.
Can anyone help, please?

Thanks,
Peter Royle
 
Peter Royle said:
I need to get a 2-byte number into a string, but I DON'T want VB
to obligingly turn it into a string - in other words, I want to end
up with 2 bytes in the string, which is the number as held in the
int16 variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))

why not ...chr(no and 255)?
 
Hi Peter,

I'm not sure what the issues are with unpacking. Have a go with this:

I = &hFACE
S = Chr(I \ 256 And 255) & Chr(I And 255)
I = Asc (S.Chars (0)) * 256 + Asc (S.Chars (1))

Regards,
Fergus
 
.. . .
I need to get a 2-byte number into a string .. . .
I have tried doing stuff like
str = chr((no >> 8)) & chr((no and 256)) .. . .
this is giving me problems unstringing it again

The "encoding" looks fine to me - what problems are are getting
at the other end? If it's an overflow problem, you might try this:

no = Val("&H" _
& Right("0" & Hex(Asc(str.Substring(0, 1))), 2) _
& Right("0" & Hex(Asc(str.Substring(1, 1))), 2) _
)

HTH,
Phill W.
 
(e-mail address removed) (Peter Royle) scripsit:
I need to get a 2-byte number into a string, but I DON'T want VB to
obligingly turn it into a string - in other words, I want to end up
with 2 bytes in the string, which is the number as held in the int16
variable, not an ASCII representation of its value.

I have tried doing stuff like

str = chr((no >> 8)) & chr((no and 256))

\\\
Dim i As Short = 123
MsgBox(i.ToStrint())
///
 
(e-mail address removed) (Peter Royle) scripsit:

\\\
Dim i As Short = 123
MsgBox(i.ToStrint())
///

I thought my original message was clear enough, but apparently not.
This is a beautiful example of a fundamental technique that I learnt
about 5 years ago - putting a number into a string - and it's wrong -
what is Strint()? Fortunately, the other responders have been helpful.
 
(e-mail address removed) (Peter Royle) scripsit:
I thought my original message was clear enough, but apparently not.
This is a beautiful example of a fundamental technique that I learnt
about 5 years ago - putting a number into a string - and it's wrong -
what is Strint()? Fortunately, the other responders have been helpful.

Sorry for the typo... I misunderstood you (I am not a native English
speaker). Why not use 'Chr' or 'ChrW' directly?
 
(e-mail address removed) (Peter Royle) scripsit:


Sorry for the typo... I misunderstood you (I am not a native English
speaker). Why not use 'Chr' or 'ChrW' directly?

Herfried - I understand perfectly. I was getting a bit wound up
yesterday - sorry!

I have tried using ascW and chrW (it is a 2-byte value) - it works on
the PC, but fails on the Pocket PC - Argument out of range. That's why
I am trying to manipulate the bytes separately - but as soon as you
use Asc, you get a signed, 2-byte integer - and so, I think, the
Pocket PC software is seeing the leftmost bit as a sign bit. So if I
send it 127 or 256, it extracts them fine. Any value with a 1 in the
leftmost bit - 128-255, for example - is just seen as 63. I can do
this easily on the PC - it's the Pocket PC that is fouling up,
somehow.

Peter
 
(e-mail address removed) (Peter Royle) scripsit:


Sorry for the typo... I misunderstood you (I am not a native English
speaker). Why not use 'Chr' or 'ChrW' directly?

A follow up to my last posting - the problem was not "Argument out of
range" - that was my doing. Now, using ChrW and AscW, it sees anything
over 127 wrongly. Again, it all works fine on the PC!!

Peter
 
Back
Top