Solved

How to convert 16 bit byte to 2 string characters

Posted on 2008-10-29
6
1,164 Views
Last Modified: 2012-05-05
I have a C# app that reads binary data from an app through a C++ import.

The data i am trying to read is held in short integers so i need to split the int16 into its byte pairs and read the values into its two strings.

I do it in VB.net with the following function
 Public Shared Function ByteToString(ByVal bytStr As Byte) As String
        Dim strCon As String        'hold the string
        strCon = Convert.ToChar(bytStr)
        'return the string
        Return strCon
    End Function

But i cannot find the same functionality in C#.

Supposedly this should work but all i store then is the numeric value of the byte
bytByteArr[1].ToString();

Thanks for any help!!
0
Comment
Question by:Halon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 

Author Comment

by:Halon
ID: 22836177
Little more info

I read in from a byte array 2 bytes at a time
'read the next 2 bytes in                  
                bytByteArr = brContents.ReadBytes(2);

for example first two bytes are 73 and 114 - which are the letters I and r
Then i get 101 and 110 - which are e and n respectively

Hope this helps my clear as mud question
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22836332
If you have a 16-bit integer:

int input = 1234;
int lower = input & 0x00FF;
int upper = (input & 0xFF00) >> 4;
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22836371
Sorry, that should be a shift of 8-bits not 4!

If you have a 16-bit integer:

int input = 1234;
int lower = input & 0x00FF;
int upper = (input & 0xFF00) >> 8;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22836427
If you have an array of bytes and you want to convert them to a string you can use the following:
// You get this from the file.
byte[] bytes = new byte[] { 73, 114 };
// Then convert byte array to string, you can use a different encoding if you need to.
string result = System.Text.Encoding.Default.GetString(bytes);

Open in new window

0
 

Author Comment

by:Halon
ID: 22841416
numberkruncher, that works great. i think i am taking the long way at getting what i need done but your idea gets my string out of the binary data.

A question though. I know the field i am grabbing can be up to 20 ints long, thats 40 short ints that i grab. At any point there can be null terminators within the data ('\0' i belive is the code from C++ i am grabbing). How can i strip this from my string or toss it as i create it?

Thanks!!
0
 

Author Comment

by:Halon
ID: 22841747
Its OK, i found it.

//set the char we want to trim, in this case the nulls
        char[] trimNulls = { '\0' };

        //return the result
        return strTextResult.Trim(trimNulls);

Thanks!!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question