473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Data conversion

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
Nov 20 '05 #1
9 2450
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
Nov 20 '05 #2
"Peter Royle" <pj*****@tiscali.co.uk> wrote in message
news:fc*************************@posting.google.co m...
.. . .
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.
Nov 20 '05 #3
"Peter Royle" <pj*****@tiscali.co.uk> schrieb
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)?
but this is giving me problems unstringing it again, on a Pocket
PC. Can anyone help, please?


Nov 20 '05 #4
pj*****@tiscali.co.uk (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())
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message news:<ei**************@TK2MSFTNGP11.phx.gbl>...
pj*****@tiscali.co.uk (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())
///


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.
Nov 20 '05 #6
"Armin Zingler" <az*******@freenet.de> wrote in message news:<ef*************@tk2msftngp13.phx.gbl>...
"Peter Royle" <pj*****@tiscali.co.uk> schrieb
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)?

Armin, you're quite right. This appears to be a typo. Sorry!

Peter
Nov 20 '05 #7
pj*****@tiscali.co.uk (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())
///


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?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message news:<OL**************@TK2MSFTNGP10.phx.gbl>...
pj*****@tiscali.co.uk (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())
///


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?


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
Nov 20 '05 #9
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message news:<OL**************@TK2MSFTNGP10.phx.gbl>...
pj*****@tiscali.co.uk (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())
///


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?


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
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dillon Mantle | last post by:
HI All Does any one know how to convert a binary file into a readable (by human) file? Its from a bin file, has addresses and stuff which I can work out... but how do you do the actual...
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
6
by: New MSSQL DBA | last post by:
Hi all, we are now planning to upgrade our application from a non-unicode version to a unicode version. The application's backend is a SQL Server 2000 SP3. The concern is, existing business...
4
by: Ivan | last post by:
I am running a statically bound COBOL program on DB2 7.1 OS/390 trying to insert EBCDIC strings into a varchar column in a table on DB2 UDB 7.2 FP10a Workgroup edition. The varchar column is...
4
by: wb | last post by:
Newbie at .net having trouble with data conversion. I am using the random constructor and trying to pass in seed value of time as instructed by help. rvalue = new random(int) dim x as long...
1
by: tony.fountaine | last post by:
I am working on a project to read a Bosch Measurement Data File (MDF). The file contains a number of blocks that can be read from the file using a baisc structure. For example the ID BLOCK is as...
0
by: bpo_ccs | last post by:
We are into BPO and Software development business for past six and half years. We are looking for the following any kind of business from your end. Back Office Process Data Entry, Large Volume...
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
0
by: dataentryoffshore | last post by:
Get a Discount up to 60% on data entry, data capture, dataentry services, large volume data processing and data conversion services through offshore facilities in India. Offshore data entry also...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.